Cpp Tutorial: C++ Size of an object in C++

1. Where are member functions stored in C++
2. Introduction to size of object.
3. Size of object without structure padding
4. Size of object with structure padding
5. Size of object with static data members.
6. Size of object with virtual function
7. Size of object with Inheritance
8. Size of Empty Class in C++?
9. Size of Empty Class with virtual function?

1. Where are member functions stored in C++

The member function code will be stored in Code segment of the program. So how may objects you create, there will be only 1 copy of the member functions and all the objects will refer to that code segment address.

2. Introduction to size of object.

In simple terms the size of the object is equal to the total size of non-static data members and vptr.
In the next series of examples, we shall see how the object size differs in different conditions.

3. Size of object without structure padding

In the below example we shall see the size of the object without structure padding.
/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Example
{
        int i_num;
        char c_num;
        int i_num1;
        char c_num2;
};

int main()
{
    Example e;
    cout<<"The size is "<< sizeof(e)<<endl;
}
Output:
The size is 16

As there is structure padding happening, so
        int i_num;   = 4 bytes
        char c_num;   = 4 bytes
        int i_num1;    = 4 bytes
        char c_num2;   = 4 bytes

4. Size of object with structure padding

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Example
{
        int i_num;
        int i_num1;
        char c_num2;
        char c_num;

};

int main()
{
    Example e;
    cout<<"The size is "<< sizeof(e)<<endl;
}
Output:
The size is 12

As there is no structure padding, the 
        int i_num;   = 4 bytes
        char c_num;   = 4 bytes
        int i_num1;    = 2bytes
        char c_num2;   = 2 bytes

5. Size of object with static data members.

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Example
{
       static int i_num;
        int i_num1;
};

int main()
{
    Example e;
    cout<<"The size is "<< sizeof(e)<<endl;
}
Output:
The size is 4
As static data member will be stored in shared storage, only non-static members will be present in the object.

6. Size of object with virtual function

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Example
{
        int i_num1;
        
        virtual void display()
        {
            cout<<"this is a function";
        }
};

int main()
{
    Example e;
    cout<<"The size is "<< sizeof(e)<<endl;
}
Output:
The size is 14
Here
int will take 4 bytes.
_vptr will take 12 bytes

7. Size of object with Inheritance

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Base
{
    public:
        int i_num1;
        
};

class Derived: public Base
{
    public:
        int i_num2;
        
};

int main()
{
    Derived d;
    cout<<"The size is "<< sizeof(d)<<endl;
}
Output:
The size is 8
Here
4 bytes from base class int variable
4 bytes from derived class int variable

8. Size of Empty Class in C++?

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Base
{
        
};

int main()
{
    Base b;
    cout<<"The size is "<< sizeof(b)<<endl;
}
Output:
The size is 1
Why the empty object size is 1?
Because if a class has multiple objects, each will have unique memory location. So if a class is empty what will be stored in that location?
So the least memory reserved will be 1.

9. Size of Empty Class with virtual function?

/*
* Copyright: @ prodevelopertutorial.com
*/

#include <iostream>
using namespace std;

class Base
{
virtual void display()
{
}
        
};

int main()
{
    Base b;
    cout<<"The size is "<< sizeof(b)<<endl;
}

Output:

The size is 8

As _vptr needs 8 bytes, the size of the object is 8 bytes.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *