[DGD] A trivial query about the behaviour of local variables

Par Winzell zell at skotos.net
Fri Apr 23 16:41:19 CEST 2004


Robert,

> void pointless() {
>    int i; /* Allocate some memory to hold i */
> } /* De-allocate the memory */
> 
> void pointless() {
>    int i; /* Allocate some memory to hold i if the function is called 
> for the first time, or hasn't
>            * been called recently.
> }

The memory is allocated when the function is entered. It's allocated on 
the stack, along with things like function parameters. Remember that as 
the execution thread winds through your code, it might return to this 
function many times -- most obviously in the case of plain recursion -- 
and many copies of 'i' must be stored, each one unique for each level of 
recursion.

An introductory textbook on this subject would do MUCH better at 
explaining the various choices available to a language designer than any 
reply on this list.

Zell
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list