Have you ever waved it so slightly.
Yes, local ( auto ) variables are usually stored on the stack. However, when reading, they are not popped out of the stack; they are referenced by the offset from the stack pointer.
Take the following code:
x = y + z;
where each of x , y and z is allocated on the stack. When the compiler generates equivalent machine code, it will refer to each variable to the offset from the given register, for example:
mov -8(%ebp), %eax add -12(%ebp), %eax mov %eax, -4(%ebp)
In x86 architecture, %ebp is the frame pointer; the stack is divided into frames, where each frame contains the parameters of the function (if any), the return address (that is, the address of the command following the function call), and local variables (if any). On systems that I'm familiar with, the stack grows “down” to 0, and local variables are stored “below” the frame pointer (lower addresses), hence a negative offset. The code above assumes that x is at -4(%ebp) , y is at -8(%ebp) , and z is at -12(%ebp) .
When the function returns, but not earlier, everything will be removed from stack 1 .
EDIT
Note that none of this is provided by the C language definition. The language does not require the use of a runtime stack at all (although the compiler will be a bitch to implement without it). It simply defines the lifetime of auto variables as from the end of their declaration to the end of their scope. The stack makes this easy, but it is not required.
1. Well, the stack and frame pointers will be set to new values; the data will remain where it was, but that memory is now available for something else. source share