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

Daniel Sloan Daniel.Sloan at vu.edu.au
Fri Apr 23 16:16:33 CEST 2004


On Fri, 2004-04-23 at 22:43, Robert Forshaw wrote:
> Someone who knows the DGD code well should be able to answer this. I'm 
> merely curious more than anything else.

I can't speak on behalf of the DGD code, since I don't know  it that
well...I can speak generally in terms of this sort of issue, but you
never know what sort of funky stuff Dworkin might've come up with ;-)

> I understand from the above code that the data stored in variable i is gone 
> for good once the function has finished. What I would like to know is 
> whether the memory space which was used for the variable i will remain 
> allocated for any length of time after the function ends, to be re-used for 
> the same variable if the function is called again. To me this would seem 
> more efficient than simply allocating memory for the local variables at the 
> start of the function and deallocating it at the end.

There are a number of disadvantages to the approach of statically
allocating per local variable, the way that you are suggesting.
Primarily, keep in mind that there are a LOT of local variables, and you
aren't trying to actually save data - if anything, you are just saving
the allocation space itself, and the time taken to allocate/deallocate. 
Of course, assuming you never deallocate the space, you *might* be
saving the memory allocation system some pain in terms of small block
fragmentation, etc (depending on how good the allocator is)...but the
tradeoff is a massive amount of memory used, and most of it sitting
there doing nothing, with no useful data in it.

So, since you aren't trying to save the data itself, and your actively
used local variable memory space is highly variable in both the types,
amount, and content of the data, there is little point in keeping
(exactly) the same space for the same variables, even just for
frequently used ones.

Instead, the best solution I know of is a stack of some type; a dynamic
memory space that stores whatever currently existing local variables
there are.

Hmm...why am I even writing all this down? Do a quick google (search for
'data structure stack'): you'll bring up stuff like this:
http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/stacks.html

You were actually basically on the right track, which is good to see...

- Dan.

> 
> <snipped>

> _________________________________________________________________
> Sign-up for a FREE BT Broadband connection today! 
> http://www.msn.co.uk/specials/btbroadband
> 
> _________________________________________________________________
> List config page:  http://list.imaginary.com/mailman/listinfo/dgd

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



More information about the DGD mailing list