Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What is the size of an object of an empty class?

Tags:

c++

object

class

People also ask

What is size of empty structure in C?

Sizeof empty struct is 0 byte in C but in C++ it is 1 byte.

What is the size of an object of a class?

The size of object of a class depends on the no. of bytes occupied by the data members of the class. }; The object of class student will occupy space of 8 bytes.

What is a size of empty class in C++? *?

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

What is an empty class in C++?

C++ classes are often "empty," which means that their internal representation does not require any bits of memory at run time. This is the case typically for classes that contain only type members, nonvirtual function members, and static data members.


Quoting Bjarne Stroustrup's C++ Style and Technique FAQ, the reason the size is non-zero is "To ensure that the addresses of two different objects will be different." And the size can be 1 because alignment doesn't matter here, as there is nothing to actually look at.


The standard states that all most derived objects have sizeof() >= 1:

Unless it is a bit-field (class.bit), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class sub-objects may have zero size. ISO/IEC FDIS 14882:1998(E) intro.object


That's really an implementation detail. Once long ago, I thought it could be zero bytes or a thousand bytes, that it has no bearing on the language specification. But, after looking at the C++17 standard (expr.sizeof), sizeof is defined as always returning one or greater, no matter what.

The size of a most derived class shall be greater than zero.

This is required for, among other things, allowing you to handle arrays of objects and pointers to them. If your elements were allowed to be zero-sized then &(array[0]) would be identical to &(array[42]), which is going to cause all sorts of havoc to your processing loops.

The reason why it may not be a machine word is that there are no elements within it that actually require it to be aligned on a word boundary (such as an integer). For example, if you place char x; int y; inside the class, my GCC clocks it at eight bytes (since the second int must be aligned in that implementation).


Having said that, that particular wording appears to have been removed from C++20, allowing for at least the possibility of objects that can take up no space. However, the following text has been added to that same section:

When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array.

Since arrays need to be able to distinguish between elements, that would mean sizeof would have to return at least one, even if the object itself technically took up no space.

So, different wording, but the same overall effect.


Even though its not required to assign any memory for an empty class, but in order to make objects of empty classes, compiler assigns the minimum memory that can be assigned, which is 1 byte. This way compiler can distinguish two objects of the same empty class uniquely, and will able to assign the address of the object to a pointer of the empty class type.


I think it might be helpful to link to an answer explaining this good too. It is about boost::compressed_pair by Logan Capaldo.


There is an exception: 0-length arrays

#include <iostream>

class CompletlyEmpty {
  char NO_DATA[0];
};

int main(int argc, const char** argv) {
  std::cout << sizeof(CompletlyEmpty) << '\n';
}