On Tue, 1 Oct 2002, P. Suckling wrote:
> The following two modules a) and b)...
>
<snip>
>
> However, would use of one of either a) or b) actually require less stack
> space and therefore be more memory efficient? Does a) require two separate
> copies of the variables a and b in memory; one for each procedure
> do_something and do_something else, whereas b) only needs one copy for the
> whole module? Also, as for variable c, is it the case that: For a) it is
> added to the stack as procedures do_something and do_something else are
> called, and removed when the procedures return, whereas in b) its memory is
> always reserved on the stack?
>
> I realise this might be a compiler dependent question, but it would be handy
> if someone could give me a feeling for how allocation of stack space works
> with modules and procedures general.
>
To add to what the other two respondents have already said, I should point out
that depending on the application and the compiler, non-static local variables
can sometimes have a major performance advantage when the subroutine is called
within a loop -- the compiler can see that there is no inter-iteration
dependence, and vectorization or other performance improvements become
possible.
In other words, don't be a slave to memory efficiency or you'll be shooting
yourself in the foot when Moore's law comes along to lift your boat. ;-)
Ted
--
Ted Stern Engineering Applications
Cray Inc. http://www.cray.com
411 First Avenue South, Suite 600 206-701-2182
Seattle, WA 98104-2860 FAX: 206-701-2500
Debuggers' motto: Frango ut patefaciam -- I break in order to reveal
|