Code 1:
int main(){
int a=1;
int b=2;
cout << "&a: "<<&a << endl;
}
Output 1:
&a: 0x22ff48
Code 2:
int main(){
int a=1;
int b=2;
cout << "&a: "<<&a << endl;
cout << "&b: "<<&b << endl;
}
Output 2:
&a: 0x22ff4c
&b: 0x22ff48
So My Question is why the address of the varibale a changed when I printed out the address of the varibale b ?
When you didn’t use
bat all, the compiler probably removed it completely, so it didn’t occupy any space.When you took the address of
b, that forced the compiler to allocate space for it.