I have a question about the memory allocation of class. My compiler is GCC 4.1.2.
I define a class called Foo, create three objects of it, and have a look at their addresses.
Following is my code:
class Foo {
int k;
};
int main()
{
Foo f1, f2, f3;
cout << sizeof(Foo) << endl; //output : 4
cout << (long int)&f1 << endl; //output : 140734093566448
cout << (long int)&f2 << endl; //output : 140734093566432
cout << (long int)&f3 << endl; //output : 140734093566416
return 0;
}
Why the gap of the addresses of the objects is constantly 16 (I tried many times) while the size of Foo is only 4? Why does the compiler arrange the memory like this? What is the use of the rest 12 memory sites? Thank you.