VOOZH about

URL: https://www.geeksforgeeks.org/cpp/why-is-the-size-of-an-empty-class-not-zero-in-c/

⇱ Size of an Empty Class in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Size of an Empty Class in C++

Last Updated : 6 Aug, 2025

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

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

Empty class: It is 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. 

Why actually an empty class in C++ 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 1 byte is allocated by the compiler to an object of an empty class for its unique address identification. 

If a class has multiple objects they can have different unique memory locations. 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 a 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 a unique address.


Output
Size of Empty Class is = 1

The 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. 


Output
Fine 

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


Output
Fine 

Now, guess the output of the following program:


Output
4

Note: 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.  


Output
sizeof(Empty) 1
sizeof(Derived1) 1
sizeof(Derived2) 8
sizeof(Derived3) 1
sizeof(Derived4) 16
sizeof(Dummy) 1


Comment
Article Tags:
Article Tags: