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.

Recommended Answers

All 6 Replies

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.

I get gaps of 4, not 16, when I run it on Dev C++ 4.9.9.2 on Windows XP.

I get gaps of 4, not 16, when I run it on Dev C++ 4.9.9.2 on Windows XP.

Then why does gcc do this? Any benefit? The rest 12 memory sites are useless?

It is interesting that if the member variable is char instead of int, the gap is exactly 1, the same as the size of the class.

class Bar {
    char c;
};
int main()
{
    Bar b1, b2, b3;
    cout << sizeof(Bar) << endl;  //output : 1
    cout << (long int)&b1 << endl; //output : 140736836889151
    cout << (long int)&b2 << endl; //output : 140736836889150
    cout << (long int)&b3 << endl; //output : 140736836889149
    return 0;
}

It is interesting that if the member variable is char instead of int, the gap is exactly 1, the same as the size of the class.

class Bar {
    char c;
};
int main()
{
    Bar b1, b2, b3;
    cout << sizeof(Bar) << endl;  //output : 1
    cout << (long int)&b1 << endl; //output : 140736836889151
    cout << (long int)&b2 << endl; //output : 140736836889150
    cout << (long int)&b3 << endl; //output : 140736836889149
    return 0;
}

I'm not sure you can ever assume anything as far as what addresses will be set aside by the compiler in cases like this. You are not promised that the memory will be contiguous, so the compiler/computer is free to give you any memory location it chooses. It gave me contiguous memory, but one should never assume it will. How memory addresses are assigned and why is well beyond my knowledge base. There are some compiler experts on this forum, but I'm definitely not one of them by a long shot. :) Maybe one of them will chime in. I'm curious as well.

I've tried the above using tests with unsigned __int8, __int16, __int32 and __int64 (for the class and cast) and received either contiguous or non-contiguous results.

More likely than not, we missed something (like padding factor for certain values), though I can't confirm this...

*Heads for the C++ Object Model*

>_>

Define destructor , constructor and see whether you see different result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.