Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can stack memory be allocated within a function automatically?

I'm sorry if this has been asked before, but I didn't find anything...

For a "normal" x86 architecture:

When I call a large function in C++, is the memory then allocated immediately for all stack variables? Or are there compilers which can (and do) modify the stack size even if the function is not finished.

For example if a new scope starts:

int largeFunction(){
    int a = 1;
    int b = 2;

    // .... long code ....

    { // new scope

         int c = 5;

         // .... code again ....

    }

    // .....

}

Could the call stack "grow" also for the variable c at the beginning of the separate scope and "shrink" at its end? Or will current compilers always produce code which affects the stack pointer at the entry and return value of the function? Thanks for your answer in advance.

like image 726
Ben Avatar asked Oct 10 '13 17:10

Ben


People also ask

How is stack memory allocated?

Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack.

Is stack automatic memory?

Another feature is that memory on the stack is automatically, and very efficiently, reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required.

When should you allocate stack for a function?

Use the stack when your variable will not be used after the current function returns. Use the heap when the data in the variable is needed beyond the lifetime of the current function.

Can memory from stack be allocated at run time?

Stack memory is allocated at runtime. Keep in mind that it has to be allocated at runtime, as there is no way for the compiler to know how many times a function is called, or how many times a while loop is executed, or whatever.


1 Answers

1) How long a function is has nothing to do with the allocation of memory, independent of stack or heap.

2) When stack is "allocated" depends only on the compiler's way to make the most efficient code. "Efficient" has a wide range of requirements. All compilers have options to modify the optimizer goals for speed & size and most compilers can optimize also for lower stack consumption and other parameters.

3) Automatic variables can go on the stack but that is not a must. A lot of variables should be "allocated" to registers of your cpu. This speeds up the code a lot and saves stack. But this depends very much on the cpu platform.

4) When a compiler generates a new stack frame is also a question of optimization of code. Compilers can do "out of order execution" if this saves resources or fits better with the architecture. So the question when a stack frame comes in use cannot be answered. A new scope (open brace) can be the point for allocating a new stack frame, but this is never a guarantee. Sometimes it is not efficient to do a recalculation of all stack relative addresses of all called functions from the actual scope.

5) Some compilers can also use heap memory for auto variables. This is often seen on embedded cores if access via special instructions is faster as a stack relative addressing.

But normally it is not very important when a compiler do what he wants. The only thing which is sometimes to remember is, that you have to guarantee that your stack is large enough. Often system calls for new threads have params to set the stack size. So you have to know how many stack size your implementation needs. But in all other cases: Forget to think about. This job is done perfectly from your compiler developers.

like image 133
Klaus Avatar answered Nov 12 '22 10:11

Klaus