Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ class empty class size 1 byte

Tags:

c++

sizeof

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???

like image 414
Sreyan Avatar asked Feb 06 '12 07:02

Sreyan


People also ask

Why is the size of an empty class 1 byte?

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.

What is the 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 empty class in bikes?

It is known that size of an empty class is not zero. Generally, it is 1 byte.

What is the size of an empty function in C++?

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.


3 Answers

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.

like image 158
Luchian Grigore Avatar answered Sep 24 '22 22:09

Luchian Grigore


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.

like image 21
Matthieu M. Avatar answered Sep 22 '22 22:09

Matthieu M.


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

like image 24
Michael Anderson Avatar answered Sep 20 '22 22:09

Michael Anderson