|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
In a recursive program, do you think it is necessary to add the static
keyword for every variable or do you think it is unnecessary ? Is every call associated with seperate copy of variables ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
pereges said:
> In a recursive program, Presumably you mean a recursive function. > do you think it is necessary to add the static > keyword for every variable No. In fact, to do so defeats the whole point of recursion. In general, for any object use the smallest scope and the shortest lifetime that you can get away with. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
>In a recursive program, do you think it is necessary to add the static
>keyword for every variable It is probably necessary to *REMOVE* the static keyword for most of the variables. >or do you think it is unnecessary ? Is >every call associated with seperate copy of variables ? If you use static, you get *one* copy of the variable for all of the calls *combined*. This is not what you want for a recursive program, although static read-only variables are not a problem. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"pereges" <Broli00@gmail.com> wrote in message news:69ccb072-b245-4b6c-abfc-31589d11fbf7@c19g2000prf.googlegroups.com... > In a recursive program, do you think it is necessary to add the static > keyword for every variable or do you think it is unnecessary ? Is > every call associated with seperate copy of variables ? > It's very wise to make every variable that is not conserved across recursive calls static. Otherwise you will get a separate instance on each call, which could waste significant amounts of memory. However in the nature of things, most variables will need to be local. Otherwise you probably wouldn't be tackling the problem recursively. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
gordonb.2o4kx@burditt.org (Gordon Burditt) writes:
> pereges <Broli00@gmail.com> writes: >>In a recursive program, do you think it is necessary to add the static >>keyword for every variable > > It is probably necessary to *REMOVE* the static keyword for most > of the variables. > >>or do you think it is unnecessary ? Is >>every call associated with seperate copy of variables ? > > If you use static, you get *one* copy of the variable for all of the > calls *combined*. This is not what you want for a recursive program, > although static read-only variables are not a problem. That may or may not be what you want. It depends on what you're doing. If you want a single copy of a variable for all invocations of a function, use "static". If you want a separately allocated copy for each invocation, don't use "static". Recursion has very little to do with the choice. If you find yourself using "static" a lot, re-think what you're doing; the vast majority of variables declared within functions should *not* need to be static. Note: Gordon deliberately and rudely deleted the attribution line for "pereges", as he always does. I've re-inserted it. I do not grant permission to quote this or any other article I post to Usenet without attribution. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Keith Thompson" <kst-u@mib.org> wrote in message > If you want a single copy of a variable for all invocations of a > function, use "static". If you want a separately allocated copy for > each invocation, don't use "static". Recursion has very little to do > with the choice. > Conside this simple function void zero(NODE *node) { int i; for(i=0;i<node->N;i++) node->x[i] = 0; } it would be very confusing to make i static, though almost harmless - you gobble an extra few bytes of memory. Now let's make it recursive void zeror(NODE *node) { static int i; if(!node) return; for(i=0;i<node->N;i++) node->x[i] = 0; zeror(node->left); zeror(node-<right); } now there is a point in making i static. Unless the compiler is extremely good it won't realise that a separate instance of i is not required for each call. So we can handle a rather deeper tree than if i is automatic. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Malcolm wrote:
) void zeror(NODE *node) ) { ) static int i; ) ) if(!node) ) return; ) for(i=0;i<node->N;i++) ) node->x[i] = 0; ) zeror(node->left); ) zeror(node-<right); ) } ) ) now there is a point in making i static. Unless the compiler is extremely ) good it won't realise that a separate instance of i is not required for each ) call. So we can handle a rather deeper tree than if i is automatic. If the compiler is even remotely decent it will put i in a register and never allocate memory for it in the first place. Making it static might prevent that from happening. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Malcolm McLean wrote:
> .... snip ... > > Conside this simple function > > void zero(NODE *node) { > int i; > > for (i = 0; i < node->N; i++) node->x[i] = 0; > } > > it would be very confusing to make i static, though almost > harmless - you gobble an extra few bytes of memory. No, it would be highly harmful. Consider: NODE n1, n2; ... zero(&n1); ... /* some interrupt routine */ ... zero(&n2); ... Would because of the 'static', require forbidding interrupts during execution of zero(&n1). -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. ** Posted from http://www.teranews.com ** |
|
![]() |
| Outils de la discussion | |
|
|