Why is the Size of an Empty Class Not Zero in C++?
Last Updated :
22 Nov, 2021
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.
Now to understand the size of an empty class, let's learn what is empty class is first!
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.
The code below shows the Size of the Empty Class:
CPP
// C++ program without any compilation
// error to demonstrate the size of
// an Empty Class
#include <iostream>
using namespace std;
// Creating an Empty Class
class Empty_class {
};
// Driver Code
int main()
{
cout << "Size of Empty Class is = "
<< sizeof(Empty_class);
return 0;
}
OutputSize 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.
CPP
// C++ program without any compilation
// error to demonstrate that the size
// of the two different objects of an
// Empty Class will have different
// addresses
#include <iostream>
using namespace std;
// Creating an Empty class
class Empty {
};
// Driver Code
int main()
{
Empty a, b;
if (&a == &b)
cout << "Impossible " << endl;
else
cout << "Fine " << endl;
return 0;
}
For the same reason (different objects should have different addresses), 'new' always returns pointers to distinct objects. See the following example.
C++
// C++ program without any
// compilation error to demonstrate
// that "new" always returns pointers
// to distinct objects
#include <iostream>
using namespace std;
// Creating an Empty Class
class Empty {
};
// Driver Code
int main()
{
Empty* p1 = new Empty;
Empty* p2 = new Empty;
if (p1 == p2)
cout << "Impossible " << endl;
else
cout << "Fine " << endl;
return 0;
}
Now, guess the output of the following program:
CPP
// CPP Program as an exercise
#include <iostream>
using namespace std;
// Creating an Empty Class
class Empty {
};
// Creating a Derived Class
class Derived : Empty {
int a;
};
// Driver Code
int main()
{
cout << sizeof(Derived);
return 0;
}
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.
CPP
// CPP Program as an exercise
#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;
}
Outputsizeof(Empty) 1
sizeof(Derived1) 1
sizeof(Derived2) 8
sizeof(Derived3) 1
sizeof(Derived4) 16
sizeof(Dummy) 1
Similar Reads
list::empty() and list::size() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if
3 min read
Implementation of Singleton Class in C++
A singleton class is a special type of class in object-oriented programming which can have only one object or instance at a time. In other words, we can instantiate only one instance of the singleton class. The new variable also points to the initial instance created if we attempt to instantiate the
6 min read
Can a C++ class have an object of self type?
A class declaration can contain static object of self type, it can also have pointer to self type, but it cannot have a non-static object of self type. For example, following program works fine. // A class can have a static member of self type #include<iostream> using namespace std; class Test
2 min read
How to Define the Constructor Outside the Class in C++?
A constructor is a special type of member function whose task is to initialize the objects of its class. It has no return type so can't use the return keyword and it is implicitly invoked when the object is created. Constructor is also used to solve the problem of initialization. It is called after
3 min read
std::is_nothrow_copy_assignable in C++ with Examples
The std::is_nothrow_copy_assignable template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_copy_assignable template of C++ STL is used to check whether T is copy assignable type or not and this is known for not to throw any exception. It returns the boolean value
2 min read
Print "Hello World" with empty or blank main in C++
Write a program in C++ that prints "Hello World", it has a main function and body of main function is empty. Following are three different solutions. We can create a global variable and assign it return value of printf() that prints "Hello World" C/C++ Code // C++ program to print something with emp
2 min read
What is the size_t data type in C?
size_t is an unsigned integer data type that is defined in various header files such as: <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h>It's a type which is used to represent the size of objects in bytes and is therefore used as the return type
4 min read
std::is_nothrow_assignable in C++ with Examples
The std::is_nothrow_assignable template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_assignable template of C++ STL is used to check whether A is type assignable to B or not and this is known for not to throw any exception. It return the boolean value true if A i
3 min read
Why does empty Structure has size 1 byte in C++ but 0 byte in C
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The âstructâ keyword is used to create a structure. The general syntax for creating a structure is as shown below: Syntax- struct structur
3 min read
stack empty() and stack size() in C++ STL
The std::stack::size() and std::stack::empty() in C++ are built-in functions that are used to provide information about the size of the stack. They are the member functions of the std::stack container defined inside <stack> header file. stack::empty()The stack::empty() method is used to check
2 min read