Reputation: 6475
I run the follow code:
void func()
{
int i;
int array[10];
cout << &i << endl;
for(int i = 0; i < 10; ++i)
{
cout << &array[i] << '\n';
}
}
the output is:
0x28fe98
0x28fe70
0x28fe74
0x28fe78
0x28fe7c
0x28fe80
0x28fe84
0x28fe88
0x28fe8c
0x28fe90
0x28fe94
why the address of i
is 0x28fe98
?
i think the address of i
must be &array[0] - 4
.
why the address of i
is &array[9] + 4
in fact?
Upvotes: 2
Views: 164
Reputation: 13288
why the address of i is 0x28fe98?
It is located just after the end of your array
i think the address of i must be &array[0] - 4.
Why should it be ?
why the address of i is &array[9] + 4 in fact?
Because i
is after your array :)
Typically elements are pushed on the stack and their adresses decrease while the heap limit increases but no code should rely on that except if you're on a low level (asm etc..)
Upvotes: 5