Moussa Ba wrote:
What is the behavior of the program below as far as stack usage is
concerned. We use inner blocks to lexically isolate variable and to also
force the function main to release stack space after we exit a particular
scope.
nope. it's up to the compiler to when it releases the memory. i think it
can even reuse the memory.
GCC allocates all the needed memory at the beginning of the function and
releases it at the end.
> This is necessary as we start the < real > program by calling a
function pointer stored in ROM, we want the function to basically have acess
to as much stack as it needs.
declare your function (pointer) as
__attribute__((noreturn)) void fu(void);
and gcc will not save the registers before calling the function. not
sure about local functions. but if you dont use too much variables it
will use registers anyway.
you can put initializations that use much stack into a function and call
that in main, so that it gets cleaned up when the function returns.
> I am little worried about this code, as I am
not really sure that GCC behaves as I expect it to especially with
optimization turned on. I expect that when enterring the second inner block
where I referred to r3, stack usage is just 2 bytes for the loop variable.
Can I assume that GCC will put locally declared variable withing braces on
the stack and pop them as we leave the scope ?
no
The assembley language does not look like what I was expecting, it seems
like main uses 72 bytes of stack which is consistent with the fact all the
locally declared variables within main are not popped off the stack between
inner blocks.
Am I understanding this correctly ?
yes
chris