Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
If you are compiling the code as "Position Independent," then the base address of the segments can be randomized (safety feature). (Shared libraries, for example, will use this feature since they're mapped into the address space of the executable.) This will give different addresses for the variable. However, you can compile turning this feature off, forcing variables to use the same address.
#include <stdio.h>
int a;
int main(int argc, char **argv)
{
printf("%u\n", (unsigned int)&a);
}
Example using GCC:
$ gcc -o memtest memtest.c
$ ./memtest
260116504
$ ./memtest
122437656
$ gcc -fno-pie -o memtest memtest.c
$ ./memtest
4120
$ ./memtest
4120
if you are printing 5 times using a for loop then output will be same.
- Livin D'cruz September 23, 2014if you are printing 5 times by running program 5 times then o/p will be different as program reserves memory at different location when you run.