
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Size of an Empty Class in C++
The size of an object of an empty class in C++ is 1 byte as it allocates one unique address to the object in the memory. The size can not be 0, as the two objects can not have same memory allocation.
In this article, we will see an example of checking the size of an object of an empty class in C++.
Demonstrating Size of an Empty Class
In this example, we have two C++ classes. One class is an empty class while other is not an empty class. We have printed the size of objects of both the classes as output.
#include <iostream> using namespace std; class Empty {}; class NotEmpty { public: int a = 4; }; int main() { cout << "Size of Empty class: " << sizeof(Empty) << endl; Empty obj; cout << "Size of Empty object: " << sizeof(obj) << endl; cout << "Size of NotEmpty class: " << sizeof(NotEmpty) << endl; NotEmpty obj2; cout << "Size of NotEmpty object: " << sizeof(obj2) << endl; return 0; }
Output
The output of the above code is given below:
Size of Empty class: 1 Size of Empty object: 1 Size of NotEmpty class: 4 Size of NotEmpty object: 4
Advertisements