Variable memory with ampersand vs pmap
I just started to figure out with pointers in C and one stuff confused me ...
Here's a simple code:
int main ()
{
long ms = 10000000;
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
printf("Sizeof of var2 variable: %d\n", sizeof(var2) );
retpid();
millisleep(ms);
return 0;
}
And - it returns var1 and the memory address var2 (I suppose?):
$ ./address
Address of var1 variable: 797927b4
Address of var2 variable: 797927a0
Sizeof of var2 variable: 10
PID = 15885
But - when I run pmap
- I don't see these addresses here:
$ pmap -x 15885
15885: ./address
Address Kbytes RSS Dirty Mode Mapping
0000000000400000 4 4 0 r-x-- address
0000000000600000 4 4 4 rw--- address
00007fcdb6bbc000 1576 256 0 r-x-- libc-2.12.so
00007fcdb6d46000 2048 0 0 ----- libc-2.12.so
00007fcdb6f46000 16 16 16 r---- libc-2.12.so
00007fcdb6f4a000 4 4 4 rw--- libc-2.12.so
00007fcdb6f4b000 20 12 12 rw--- [ anon ]
00007fcdb6f50000 128 104 0 r-x-- ld-2.12.so
00007fcdb7160000 12 12 12 rw--- [ anon ]
00007fcdb716d000 8 8 8 rw--- [ anon ]
00007fcdb716f000 4 4 4 r---- ld-2.12.so
00007fcdb7170000 4 4 4 rw--- ld-2.12.so
00007fcdb7171000 4 4 4 rw--- [ anon ]
00007fff79780000 84 12 12 rw--- [ stack ]
00007fff797ff000 4 4 0 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------ ------ ------
total kB 3924 448 80
What am I missing here?
source to share
I think you are missing two things:
-
Your memory address space appears to be 64 bit, so
%x
should be used instead%p
. -
The variables you print are local variables, so they are located somewhere on the stack.
After you print all 8 byte addresses, you will see that they are both on the stack.
In other words, there is an 84 KB section of memory that starts at 0x00007fff79780000.
BTW, when passing a pointer to printf
, you should normally use %p
either way.
It so happened that on a 32-bit system, using %x
it gives the same result as %p
.
source to share
The prefix to the door, which was carved by a 32-bit %x
64-bit pointers, that is 7fff
, and you will find your var / s just above the bottom of the stack: 7ffff79780000
.
Use %p
to print pointers by converting them to void*
before:
printf("Address of var1 variable: %p\n", (void*) &var1);
source to share