Suppose we have these local variables:
int a = 0;
int b = 1;
int c = 2;
int d = 3;
As far as I know, these will be allocated on the system stack, like this:
| |
| 3 | d
| 2 | c
| 1 | b
|_0_| a
Does this mean that in order to get the value of a, the values of d, c and b must first be popped out of the stack? If so, where do these values go? Does this mean that accessing more recently declared variables will be faster? Or am I missing something (which I suspect is the case), and the whole thing works in some other way?
EDIT: thanks, guys!
The local variables on the stack are usually accessed relative to the so-called frame pointer, which points at the start of your stack frame. It would also be possible to do this relative to the stack pointer, but since this moves around during evaluation of expressions it is more difficult to keep track of.
In practice such variables may also be kept in processor registers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With