Determining the size of a Class Object
By Girish Shetty
There are using factors that decide the size of an object of a class in C ++. These factors are:
Size of all non-static data members
Order of data 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)
Size of all non-static data members
Only non-static data members will be counted for calculating sizeof class/object.
Class A {PRIVATE:
Float imem1;
Const int imem2;
Static int imem3;
Char imem4;
};
For an object of Class A, the size will be the size of Float imem1 + size of int imem2 + size of char imem3. static members are really not part of the class object. they won't be encoded in object's layout. <2> order of data members the order in which one specifies data members also alters the size of the class.
Class C {
Char C;
Int int1;
Int int2;
Int I;
Long L;
Short S;
};
The size of this class is 24 bytes. even though char C will consume only 1 byte, 4 bytes will be allocated for it, and the remaining 3 bytes will be wasted (holes ). this is because the next member is an int, which takes 4 bytes. if we don't go to the next (4th) byte for storing this integer member, the memory access/modify cycle for this integer will be 2 read cycles. so the compiler will do this for us, unless we specify some byte padding/packing.
If I re-write the above class in different order, keeping all my data members like below:
Class C {
Int int1;
Int int2;
Int I;
Long L;
Short S;
Char C ;};
Now the size of this class is 20 bytes.
In this case, it is storing C, the char, in one of the slots in the hole in the extra four bytes.
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 except des size of its immediate base class.
Lets take an example:
Class B {
...
Int imem1;
Int imem2 ;}
Class D: Public B
{... Int imem ;}
In this case, sizeof (d) is will also include the size of B. So it will be 12 bytes.
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 within ss the class hierarchy. that is
Class base {public:
...
Virtual void somefunction (...);
PRIVATE:
Int iamem
};
Class derived: public base {
...
Virtual void someotherfunction (...);
PRIVATE:
Int ibmem
};
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.
Compiler being used
In some scenarios, the size of a class object can be compiler specific. Lets take one example:
Class baseclass
{
Int;
Char C;
};
Class derivedclass: Public baseclass
{
Char D;
Int I;
};
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.
The reason for sizeof (derivedclass) being 16 bytes in Mc ++ 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. (one classic example is the implementation of final class in C ++ .) when we use virtual inheritance, there will be the overhead of 4 bytes for a virtual base class pointer in that class.
Class abase
{
Int imem;
};
Class bbase: Public Virtual abase {
Int imem;
}; Class cbase: Public Virtual abase {
Int imem;
}; Class abcderived: Public bbase, public cbase {
Int imem;
};
And if you check the size of these classes, it will be:
Size of abase: 4
Size of bbase: 12
Size of cbase: 12
Size of abcderived: 24
Because bbase and cbase are dervied from abase virtually, they will also have an virtual base pointer. so, 4 bytes will be added to the size of the class (bbase and cbase ). that is sizeof abase + size of int + sizeof virtual base pointer.
Size of abcderived will be 24 (not 28 = sizeof (bbase + cbase + int member) because it will maintain only one virtual base pointer (same way of maintaining virtual table pointer ).