I was asked these 2 questions during an interview. I guess the address of a static variable will be the same no matter where it is declared. It will also have the same address from run to run. Correct me if I am wrong.
If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function?
If a static variable is declared out side of a function, will it have the same address every time you run the program?
Cunningham's Law will ensure quick correction of my mistakes:
#include <stdio.h>
#ifdef GLOBAL
static int i;
#endif
// static int j;
#ifndef GLOBAL
void f(void) {
static int i;
printf("local: %p\n" , (void *) &i);
}
#endif
int main() {
#ifdef GLOBAL
printf("global: %p\n" , (void *) &i);
#else
f();
#endif
}
You need to disable address randomization to observe it:
$ sudo bash -c 'echo 0 > /proc/sys/kernel/randomize_va_space';\
gcc 1.c && ./a.out &&\
gcc -DGLOBAL 1.c && ./a.out;\
sudo bash -c 'echo 2 > /proc/sys/kernel/randomize_va_space'
local: 0x555555558034
global: 0x555555558034
If you add another global static int j
(commented out above) after the global i
then the effect goes away:
local: 0x555555558038
global: 0x555555558034
So call me maybe!?
$ ./a.out && ./a.out
global: 0x563bcce14034
global: 0x55cd4a497034