1) How long a function has nothing to do with memory allocation, regardless of stack or heap.
2) When the stack is โdistributedโ, it depends only on the compilation method to make the most efficient code. Effective has a wide range of requirements. All compilers have options for changing optimizer goals for speed and size, and most compilers can also optimize for lower stack consumption and other parameters.
3) Automatic variables can go onto the stack, but this is not necessary. A lot of variables should be "allocated" to your processor registers. This greatly speeds up the code and saves the stack. But it depends a lot on the processor platform.
4) When the compiler creates a new frame stack, it is also a matter of code optimization. Compilers can perform "idle execution" if it saves resources or is better suited to the architecture. Therefore, the question of when the stack stack is used cannot be given. A new area (open bracket) may be the point to place the new stack frame, but this is never a guarantee. It is sometimes inefficient to recalculate all relative stack addresses of all called functions from the actual volume.
5) Some compilers can also use heap memory for automatic variables. This is often seen on embedded cores if access using special instructions is faster than relative stack addressing.
But usually this is not very important when the compiler does what it wants. The only thing you need to remember sometimes is that you must ensure that your stack is large enough. Often, system calls for new threads have options for setting the size of the stack. Thus, you should know how many stack sizes your implementation requires. But in all other cases: Forget to think. This work was done perfectly from the developers of your compiler.
Klaus source share