PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > local variables in a recursive program
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
local variables in a recursive program

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 18h14   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut local variables in a recursive program

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 ?
  Réponse avec citation
Vieux 12/04/2008, 18h21   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program

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
  Réponse avec citation
Vieux 12/04/2008, 19h42   #3
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program

>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.

  Réponse avec citation
Vieux 12/04/2008, 20h16   #4
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program


"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

  Réponse avec citation
Vieux 13/04/2008, 17h08   #5
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program

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"
  Réponse avec citation
Vieux 13/04/2008, 21h14   #6
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program


"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

  Réponse avec citation
Vieux 13/04/2008, 21h19   #7
Willem
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program

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
  Réponse avec citation
Vieux 14/04/2008, 04h34   #8
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: local variables in a recursive program

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 **
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17375 seconds with 16 queries