In each loop iteration, variable j is declared again and again. Then why is its address remaining same?
- Shouldn't it be given some random address each time?
- Is this compiler dependent?
#include<stdio.h>
#include<malloc.h>
int main()
{
int i=3;
while (i--)
{
int j;
printf("%p\n", &j);
}
return 0;
}
Testrun:-
shadyabhi@shadyabhi-desktop:~/c$ gcc test.c
shadyabhi@shadyabhi-desktop:~/c$ ./a.out
0x7fffc0b8e138
0x7fffc0b8e138
0x7fffc0b8e138
shadyabhi@shadyabhi-desktop:~/c$
It is memory on the stack. It is not allocated from the heap. The stack would not change in that loop.
The reason why the address of j
never changes is because the compiler allocates memory for j
on the stack when the function is entered as opposed to when j
comes into scope.
As always, looking at some assembly code might help explain the concept. Take the following function:-
int foo(void)
{
int i=3;
i++;
{
int j=2;
i=j;
}
return i;
}
gcc converts this to the following x86 assembly code:-
foo:
pushl %ebp ; save stack base pointer
movl %esp, %ebp ; set base pointer to old top of stack
subl $8, %esp ; allocate memory for local variables
movl $3, -4(%ebp) ; initialize i
leal -4(%ebp), %eax ; move address of i into eax
incl (%eax) ; increment i by 1
movl $2, -8(%ebp) ; initialize j
movl -8(%ebp), %eax ; move j into accumulator
movl %eax, -4(%ebp) ; set i to j
movl -4(%ebp), %eax ; set the value of i as the function return value
leave ; restore stack pointers
ret ; return to caller
Let's walk through this assembly code. The first line saves the current stack base pointer so that it can be restored when the function exits, the second line sets the current top of the stack to be the new stack base pointer for this function.
The third line is the one that allocates the memory on the stack for all the local variables. The instruction subl $8, %esp
subtracts 8 from the current top of the stack pointer, the esp
register. Stacks grow down in memory so this line of code actually increases the memory on the stack by 8 bytes. We have two integers in this function, i
and j
, each of which require 4 bytes, hence why it allocates 8 bytes.
Line 4 initializes i
to 3 by directly writing to an address on the stack. Lines 5 and 6 then load and increment i
. Line 7 initializes j
by writing the value 2 into the memory allocated for j
on the stack. Note that when j
came into scope at line 7 the assembly code did not adjust the stack to allocate memory for it, that had already been taken care of earlier.
I'm sure it's obvious, but the reason why the compiler allocates the memory for all the local variables at the start of the function is because it is way more efficient to do so. Adjusting the stack each time a local variable went in or out of scope would result in a lot of unnecessary manipulations of the stack pointer for no gain.
I'm sure you can work out what the rest of the assembly code does yourself, if not post a comment and I'll walk you through it.
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