I am new to C++ and a found a peculiar feature in C++. I saw the size of an empty is 1 byte, I did some research and found out that is is done because every object must have a distinct address. But I want to know what is the content of that 1 byte that is placed. I know it does not hold the "this" pointer but is it a dummy byte or is there actually some content???
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.
Sizeof empty struct is 0 byte in C but in C++ it is 1 byte.
It is known that size of an empty class is not zero. Generally, it is 1 byte.
C++ empty() function is used to check whether the set container is empty or not. It returns true if the set container is empty (size is 0) otherwise, it returns false.
There's no content. It's just a dummy byte.
Every class
or struct
must have its sizeof
greater than 0
, ergo your behavior. It's expected and mandated by the standard.
It is mandated by the Standard that different objects of the same type should have different addresses. This in turn ensure that for any object T
, T*
acts as a unambiguous identifier of this object (for this type).
Granted, you don't often need to know if two objects really are the same or not, but sometimes (given C++ low-level access) this is either necessary or just plain convenient.
It is thus specified that no object should have a null size.
There is an exception to this rule though: when using an empty class as a base class, the compiler may choose to apply the Empty Base Optimization (EBO) is some circumstances, for example:
struct Empty {};
struct Slim: Empty {
int a;
};
static_assert(sizeof(Slim) == sizeof(int), "");
In general the size of the base class is added, but in this particular case it is not necessary. However the rule that two different objects of the same type should never have the same address still apply, and so:
struct Fat: Empty {
Empty e;
};
static_assert(sizeof(Fat) > sizeof(Empty), "");
EBO is the main reason for using private
inheritance in template situations. For example:
template <typename Allocator>
class MyClass: private Allocator {
};
This way, if it turns out that Allocator
is an empty class, there won't be any overhead. In general, it is thus often used for policies, for example the predicates you pass to map
.
The byte contains nothing, it is there to make certain other behaviors nicer. For example consider the case of empty classes contained in another.
class Empty
{ };
class TwoEmpties
{
Empty a;
Empty b;
};
You may want the addresses of the two members, &TwoEmpties::a
and &TwoEmpties::b
, to be different. For this to happen they must have size > 1. (or the compiler would have to add padding between them, which would in turn complicate the rules for when and where the compiler can add padding to classes.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With