Reputation: 367
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???
Upvotes: 10
Views: 7649
Reputation: 7610
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.
Upvotes: 0
Reputation: 355
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
Default constructor
Default destructor
Copy constructor
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);
}
Upvotes: -1
Reputation: 53
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.
Upvotes: 1
Reputation: 299810
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
.
Upvotes: 10
Reputation: 73480
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.)
Upvotes: 3
Reputation: 87396
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.
Upvotes: 2
Reputation: 170499
That's a dummy byte - constructor and destructor will be trivial, there's no "data stored".
Upvotes: 0
Reputation: 258598
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.
Upvotes: 11