Global Variable Memory Handling

Hi ,

I have one doubt in global variable memory handling. In the following code.

#include <iostream>
using namespace std;
int dog, cat, bird, fish;
void f(int pet) {
int j,k;
cout << "F() arg " << (long)&pet << endl;
cout << "F() local variable" << (long)&j << endl;
cout << "F() local variable" << (long)&k << endl;
}
int main() {
int i, j, k;
cout << "f(): " << (long)&f << endl;
cout << "dog: " << (long)&dog << endl;
cout << "cat: " << (long)&cat << endl;
cout << "bird: " << (long)&bird << endl;
cout << "fish: " << (long)&fish << endl;
cout << "i: " << (long)&i << endl;
cout << "j: " << (long)&j << endl;
cout << "k: " << (long)&k << endl;
f(1);
}

When I execute the binary in a Linux server . I got following output.

f(): 4196564
dog: 5247876
cat: 5247880
bird: 5247884
fish: 5247888
i: 140733828755444
j: 140733828755440
k: 140733828755436
F() arg 140733828755380
F() local variable140733828755396
F() local variable140733828755392

The Local variables address keeps changing as expected , but the global variables address remains the same. Even if I Copy the source file to different location and copiled/executed the binary in a different server( LINUX). Global variables address still remains the same. I'm not clear why this global variables address are not changing. I thought once the binary is executed all the storage will be cleared...
I think that is just a coincidence. Well, I suppose it's probably not really a coincidence, it's up to the OS.
Last edited on
Topic archived. No new replies allowed.