Please refer to the below program:
#include<stdio.h>
int main()
{
int a, b;
printf("address of main =%p\n", main);
a=3;
printf("Address of 'a' =%p\n", &a);
return 0;
}
I compiled the above program using gcc
and then ran the binary. I am getting the below output:
[root@localhost gdb]# ./a.out
address of main =0x400536
Address of 'a' =0x7ffc4802cbdc
[root@localhost gdb]# ./a.out
address of main =0x400536
Address of 'a' =0x7ffe2bdcd66c
[root@localhost gdb]#
Same source code compiled with –m32
, now I'm getting the output:
[root@localhost gdb]# ./a.out
address of main =0x804841b
Address of 'a' =0xffa6b29c
[root@localhost gdb]# ./a.out
address of main =0x804841b
Address of 'a' =0xff9b808c
Here is my question: why the address of a
variable range has changed while running 64 and 32 bits application in 64 bit kernel?. The main
function address remains unchanged, Why does a
variable addresses change every run? And where is the address of a
variable stored?