Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the stack grown only when entering a function?

Tags:

c

In C, one can declare variables inside blocks. Is the space for variables declared inside these blocks allocated (i.e. the stack expands) when entering the function, or only later, when entering the block?

I know I could answer the question by firing up a debugger and looking at the disassembly, but I'm interested in knowing if the behavior is consistent across different platforms/compilers, and whether it can change as a result of optimizations.

EDIT: The answer is "it's implementation defined". It seems most implementation would indeed only do it when entering the function, excepted for dynamic-size arrays and alloca(). See the comments of this question and of the accepted answer for more details.

like image 720
Norswap Avatar asked Jan 16 '15 21:01

Norswap


People also ask

Does the stack grow up or down when a function loads?

The stack grows down (on x86). However, the stack is allocated in one block when the function loads, and you don't have a guarantee what order the items will be on the stack. In this case, it allocated space for two ints and a three-int array on the stack.

What is the use of stack in C?

Let us see for what all purposes C uses stack mainly: For storing a function’s local variables (a.k.a auto variables) For passing parameters to a function For passing the return address to a callee function (invoked function)

Can we access any element of the stack at any given time?

An array is one of the simplest containers offering random access to users based on indexes. But can we access any element of the stack at any given time? No. That’s why we need to set an index as top and then access only the element at index top. Here, 10 is a pre-defined capacity of the stack.

What is the direction in which a stack grows?

The direction is which stacks grow is architecture specific. That said, my understanding is that only a very few hardware architectures have stacks that grow up. The direction that a stack grows is independent of the the layout of an individual object.


1 Answers

From the perspective of the C programming language, all that's guaranteed is that variables with automatic storage duration (local variables) will have their memory created and destroyed as appropriate when the program is run. To the best of my knowledge, it's completely implementation defined if and when the memory for each variable will be allocated and deallocated.

In the extreme, variables might not even get memory assigned to them if they're local variables that can be stored inside registers. In that case, many different variables might actually have the same location in memory even though technically speaking they all exist at the same time, provided that the compiler could notice that the variables never actually need to co-exist. At another extreme, space for variable-length arrays can't be allocated until the size is known, so unless the compiler can do static analysis and determine that the memory for the array is needed earlier on it probably has to defer allocation until the point where the VLA is declared.

Hope this helps!

like image 177
templatetypedef Avatar answered Dec 04 '22 08:12

templatetypedef