C++ class empty class size 1 byte

落爺英雄遲暮 提交于 2019-11-28 01:00:41

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

You can use your debugger or something simple like printf("%x", *(unsigned char *)&myobj); to see the contents of the byte. I haven't read the C++ specification but I would guess that the contents of the byte are undefined so the behavior depends on the compiler and your OS.

An empty class has a sizeof 1 because when objects of that class are created they will be stored on the same location in memory if size=0.

Suppose that when you create an object the address is 1000.

If size of the class is 0 hence the size of the object must be 0 as well.

(therefore, object is located at 1000+0=1000)

So now if another object is made,

Add. of 2nd object=1000+0th location

Both the objects have same address and this is undefined behavior and shouldn't occur as there will be ambiguity to which object is being referred to.

Hence empty classes are given a byte of memory to prevent such situations from happening.

That's a dummy byte - constructor and destructor will be trivial, there's no "data stored".

Bhavith C Acharya

All the above answer is not right as per my knowledge . The correct answer is by default 4 inbuilt functions are called when object of class is created that is

  1. Default constructor

  2. Default destructor

  3. Copy constructor

  4. overloaded assignments operator

hence the size of empty class is 1byte , Example : try this

#include<iostream>
using namespace std;
class Test
{
};
int main()
{
Test t;
cout<< "size of empty class is "<<sizeof(t);
}

I faced to similar problem and it seems that one can define a class with zero length with a little trick. I do not know if it is just because of g++, but see the following code snippet:

struct ONE {};
struct ZERO { char x[0]; };

int main() {
  cout << sizeof(ONE) << ", " << sizeof(ZERO) << endl;

  ONE* po1 = new ONE;
  ONE* po2 = new ONE;
  cout << po1 << ", " << po2 << endl;

  ZERO* pz1 = new ZERO;
  ZERO* pz2 = new ZERO;
  cout << pz1 << ", " << pz2 << endl;
}

The output is:

1, 0
0xe4f010, 0xe4f030
0xe4f050, 0xe4f070

So the size of an empty class is one (according to the C++ standard), but if it has just a zero length array field the size become zero. If a new really zero sized class is allocated on the heap with new a valid address is returned and if allocated multiple times their pointers are pointing to different memory addresses.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!