Skip to content
Related Articles

Related Articles

Improve Article

Why is the size of an empty class not zero in C++?

  • Difficulty Level : Medium
  • Last Updated : 10 Jul, 2021

When structure was introduced in C, there was no concept of Objects that time. So According to C standard, it was decided to keep size of empty structure to zero. 

In C++, Size of empty structure/class is of one byte as to call function at least empty structure/class should have some size ( minimum 1 byte is required ) i.e. one byte. to make them distinguishable.

Now to understand about the size of empty class, lets learn what is empty class first!

Empty class means, a class that does not contain any data members (e.g. int a , float b, char c and string d etc.) However, an empty class may contain member functions. 

Now lets learn, why actually an empty class takes one byte?



Simply a class without an object requires no space allocated to it. The space is allocated when the class is instantiated. so to an object of an empty class, 1 byte is allocated by compiler, for it’s unique address identification. 

If a class have multiple objects they can have different unique memory location. Suppose, if a class does not have any size, what would be stored on the memory location? That’s the reason when we create an object of an empty class in C++ program, it needs some memory to get stored, and the minimum amount of memory that can be reserved is 1 byte. Hence, if we create multiple objects of an empty class, every object will have unique address.

The Code below shows the Size of Empty Class-

CPP




#include<iostream>
using namespace std;
//Creating a Empty Class
class Empty_class
{
};
// main starts
int main()
{
  cout <<"Size of Empty Class is = "<< sizeof(Empty_class);
  return 0;
}
Output
Size of Empty Class is = 1

Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses. See the following example. 

CPP




#include<iostream>
using namespace std;
 
class Empty { };
 
int main()
{
    Empty a, b;
 
    if (&a == &b)
      cout << "impossible " << endl;
    else
      cout << "Fine " << endl;
 
   return 0;
}
Output
Fine 

For the same reason (different objects should have different addresses), “new” always returns pointers to distinct objects. See the following example. 

CPP




#include<iostream>
using namespace std;
 
class Empty { };
 
int main()
{
    Empty* p1 = new Empty;
    Empty* p2 = new Empty;
 
    if (p1 == p2)
        cout << "impossible " << endl;
    else
        cout << "Fine " << endl;
 
    return 0;
}
Output
Fine 

Now guess the output of following program (This is tricky) 

CPP




#include<iostream>
using namespace std;
 
class Empty { };
 
class Derived: Empty { int a; };
 
int main()
{
    cout << sizeof(Derived);
    return 0;
}
Output
4

Note that the output is not greater than 4. There is an interesting rule that says that an empty base class need not be represented by a separate byte. So compilers are free to make optimization in case of empty base classes. As an exercise, try the following program on your compiler.  

CPP




// Thanks to Venki for suggesting this code.
#include <iostream>
using namespace std;
 
class Empty {
};
 
class Derived1 : public Empty {
};
 
class Derived2 : virtual public Empty {
};
 
class Derived3 : public Empty {
    char c;
};
 
class Derived4 : virtual public Empty {
    char c;
};
 
class Dummy {
    char c;
};
 
int main()
{
    cout << "sizeof(Empty) " << sizeof(Empty) << endl;
    cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;
    cout << "sizeof(Derived2) " << sizeof(Derived2) << endl;
    cout << "sizeof(Derived3) " << sizeof(Derived3) << endl;
    cout << "sizeof(Derived4) " << sizeof(Derived4) << endl;
    cout << "sizeof(Dummy) " << sizeof(Dummy) << endl;
 
    return 0;
}
Output
sizeof(Empty) 1
sizeof(Derived1) 1
sizeof(Derived2) 8
sizeof(Derived3) 1
sizeof(Derived4) 16
sizeof(Dummy) 1

Source: 
http://www2.research.att.com/~bs/bs_faq2.html
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :