How are variables available on the stack?

Suppose we have these local variables:

int a = 0;
int b = 1;
int c = 2;
int d = 3;

As far as I know, they will be allocated in the system stack, for example:

|   |
| 3 | d
| 2 | c
| 1 | b
|_0_| a

Does this mean that to get the value of a, the values โ€‹โ€‹of d, c and b must be popped from the stack? If so, where do these values โ€‹โ€‹go? Does this mean that access to later declared variables will be faster? Or am I missing something (which, I suspect, is), and does it all work in some other way?

EDIT: thanks guys!

+5
source share
3 answers

Local variables on the stack usually refer to the so-called frame pointer , . , , .

.

+16

-

, , - ( "" ), , .

+5

, , a, d, c b ?

The selected code simply moves the stack pointer the correct number of bytes when entering the function. When he leaves this function, he moves it at the same distance. Thus, it does not supplant variables individually. Assuming int is 4 bytes, your example would move the stack pointer 16 bytes. It actually moves it further than that due to other information in the stack frame, such as the return address.

+1
source

All Articles