memory-managementstackcompiler-constructionstack-frame

why are stack frames allocated in the static memory section if they make no calls? why the performance gain?


Are variables on the stack "statically allocated"? comes the closest but it does not answer my question.

If a procedure makes no calls then its stack frame is allocated in the static memory section where global and static variables are stored.

Why does this yield an advantage?

At runtime if stack frames are allocated on the stack are they written just like static memory?


Solution

  • In any multithreaded environment, any procedure can be active multiply whether or not it calls something.

    It takes a few instructions to push and pop a stack frame. So, if it were really true that you could stick the locals in static, you save those instructions.

    The value of this would be tiny except in some truly special circumstance in which the stack management costs were truly a significant fraction of the entire procedure -- which is very hard to believe.