Determining the Size of a Class Object
By Girish ShettyThere are many factors that decide the size of an object of a class in C++. These factors are:
- Size of all non-static da
ta members - Order of da
ta members - Byte alignment or byte padding
- Size of its immediate base class
- The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
- Compiler being used
- Mode of inheritance (virtual inheritance)
1.非静态数据成员大小
2.数据成员的顺序
3.字节长度和边界
4.直接基类的大小
5.虚函数
6.是否已经被编译
7.继承类型
Size of all non-static data members
Onclass A {For an object of class A, the size will be the size of float iMem1 + size of int iMem2 + size of char iMem4.Static members are really not part of the class object. They won't be included in object's layout. <2>Order of da
private:
float iMem1;
const int iMem2;
static int iMem3;
char iMem4;
};
sizeof(A) is4+4+4=12 ,the char iMem4 only have 1 byte,but 4 byte will be allocated for it, so the
sizeof(A)is 12byte!
class C {The size of this class is 24 bytes. Even though char c will consume on
char c;
int int1;
int int2;
int i;
long l;
short s;
};
If I re-write the above class in different order, keeping all my da
class C {Now the size of this class is 20 bytes.
int int1;
int int2;
int i;
long l;
short s;
char c;
};
In this case, it is storing c, the char, in on
Byte alignment or byte padding
As mentioned above, if we specify 1 byte alignment, the size of the class above (class C) will be 19 in both cases.Size of its immediate base class
The size of a class also includes size of its immediate base class.Lets take an example:
Class B {In this case, sizeof(D) is will also include the size of B. So it will be 12 bytes.
...
int iMem1;
int iMem2;
}
Class D: public B {
...
int iMem;
}
The existence of virtual function(s)
Existence of virtual function(s) will add 4 bytes of virtual table pointer in the class, which will be added to size of class. Again, in this case, if the base class of the class already has virtual function(s) either directly or through its base class, then this additional virtual function won't add anything to the size of the class. Virtual table pointer will be common across the class hierarchy. That isclass Base {In the example above, sizeof(Base) will be 8 bytes--that is sizeof(int iAMem) + sizeof(vptr). sizeof(Derived) will be 12 bytes, that is sizeof(int iBMem) + sizeof(Derived). Notice that the existence of virtual functions in class Derived won't add anything more. Now Derived will set the vptr to its own virtual function table.
public:
...
virtual void SomeFunction(...);
private:
int iAMem
};
class Derived : public Base {
...
virtual void SomeOtherFunction(...);
private:
int iBMem
};
Compiler being used
In some scenarios, the size of a class object can be compiler specific. Lets take onclass BaseClass {If compiled with the Microsoft C++ compiler, the size of DerivedClass is 16 bytes. If compiled with gcc (either c++ or g++), size of DerivedClass is 12 bytes.
int a;
char c;
};
class DerivedClass : public BaseClass {
char d;
int i;
};
The reason for sizeof(DerivedClass) being 16 bytes in MC++ is that it starts each class with a 4 byte aligned address so that accessing the member of that class will be easy (again, the memory read/write cycle).
Mode of inheritance (virtual inheritance)
In C++, sometimes we have to use virtual inheritance for some reasons. (Onclass ABase{And if you check the size of these classes, it will be:
int iMem;
};
class BBase : public virtual ABase {
int iMem;
};
class CBase : public virtual ABase {
int iMem;
};
class ABCDerived : public BBase, public CBase {
int iMem;
};
- Size of ABase : 4
- Size of BBase : 12
- Size of CBase : 12
- Size of ABCDerived : 24
Size of ABCDerived will be 24 (not 28 = sizeof (BBase + CBase + int member)) because it will maintain on