Copyright by By Girish Shetty

There are many 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.
C++ Code:
  1. class A {
  2. private:
  3.         float iMem1;
  4.         const int iMem2;
  5.         static int iMem3;
  6.         char iMem4;
  7. };
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. Order of data members The order in which one specifies data members also alters the size of the class.
C++ Code:
  1. class C {
  2.         char c;
  3.         int int1;
  4.         int int2;
  5.         int i;
  6.         long l;
  7.         short s;
  8. };
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:
C++ Code:
  1. class C {
  2.         int int1;
  3.         int int2;
  4.         int i;
  5.         long l;
  6.         short s;
  7.         char c;
  8. };

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 includes size of its immediate base class.

Let's take an example:
C++ Code:
  1. Class B {
  2. ...
  3.         int iMem1;
  4.         int iMem2;
  5. }
  7. Class D: public B {
  8. ...
  9.         int iMem;
  10. }
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 across the class hierarchy. That is

C++ Code:
  1. class Base {
  2. public:
  3. ...
  4.         virtual void SomeFunction(...);
  5. private:
  6.         int iAMem
  7. };
  9. class Derived : public Base {
  10. ...
  11.         virtual void SomeOtherFunction(...);
  12. private:
  13.         int iBMem
  14. };

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. Let's take one example:

C++ Code:
  1. class BaseClass {
  2.         int a;
  3.         char c;
  4. };
  6. class DerivedClass : public BaseClass {
  7.         char d;
  8.         int i;
  9. };

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++ 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. (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.

C++ Code:
  1. class ABase{
  2.         int iMem;
  3. };
  5. class BBase : public virtual ABase {
  6.         int iMem;
  7. };
  9. class CBase : public virtual ABase {
  10.         int iMem;
  11. };
  13. class ABCDerived : public BBase, public CBase {
  14.         int iMem;
  15. };

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 derived 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).