tags:

views:

2332

answers:

8

I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and point to it like any other object. But, how big is such an object?

I used this small program:

#include <iostream>
using namespace std;

class Empty {};

int main()
{
    Empty e;
    cerr << sizeof(e) << endl;
    return 0;
}

The output I got on both Visual C++ and Cygwin-g++ compilers was 1 byte! This was a little surprising to me since I was expecting it to be of the size of the machine word (32 bits or 4 bytes).

Can anyone explain why the size of 1 byte? Why not 4 bytes? Is this dependent on compiler or the machine too? Also, can someone give a more cogent reason for why an empty class object will not be of size 0 bytes?

+5  A: 

That's really an implementation detail. I originally thought it could be zero bytes or a thousand bytes, that it has no bearing on the language specification. But, after looking at the standard (section 5.3.3), it appears that sizeof must always return 1 or greater, no matter what.

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

The reason why it may not be a machine word is that there are no elements within it that 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).

paxdiablo
The reason for the "not zero" is that different objects should have different addresses. Imagine an array of zero-size objects. How would you index it? In some cases, the compiler is allowed to optimize this away though (the empty base class optmization)
jalf
@jalf: "How would you index it?" The same way I would do in C (for an array of struct objects, for example)??
Lazer
@eSKay - You couldn't if they had 0 size. They'd all be at element 0.
Brian Neal
A: 

There is nothing to stop you having a pointer or reference to something that is 0 bytes long. The memory required to store the address is held in the pointer, not the pointed to object.

I would have expected an empty class or struct to be 0 bytes. The reason for this is that I know and use the fact that the size of a class with one POD member is the size of that member, assuming #pragma pack(1). Out of interest, how big is an array of 1000 of your empty class?

Shane MacLaughlin
peterchen
@peterchen, you learn something new everyday!
Shane MacLaughlin
i did not downvote you. but your first sentence is right (in a way, at least): char *c = new char[0]; :) of course, that's somewhat contrived, but it's an example for your first sentence.
Johannes Schaub - litb
1000=>non-programmer =>-1
Behrooz
+11  A: 

The standard states that all 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

TrayMan
I find that hard to believe. The standard goes out of its way to make sure implementations have a free hand to do a good job of optimization tying the hands of the implementer like that does not sound like the kind of the thing the standard normally does (I could be wrong)
Martin York
It actually solves many problems.
Brian Neal
@Brian Neal: for example?
Lazer
@eSKay - This is required so that different objects get different addresses. You could not have a map of pointers to objects, for example, if different instances had the same address.
Brian Neal
+19  A: 

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.

Sol
Bah, what the heck would he know about C++? :-)
paxdiablo
@Sol: Does the need to "ensure that the addresses of two different objects will be different" have something to do with C++? I mean why was the need not felt for C?
Lazer
+1  A: 

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

Johannes Schaub - litb
That was very informative. Thanks! :-)
Ashwin
+1  A: 

Allocation of 1 byte for an empty class is compiler dependent. Compilers need to make sure objects reside in different memory locations and they need to allocate non zero memory size to an object. Listen to notes on this topic here: http://listenvoice.com/listenVoiceNote.aspx?id=27

Even though compilers allocates non zero size to an empty class they also do optimizations when new classes are derived from empty classes. Listen about empty base optimization on ListenVoice's c++ programming interview questions.

A: 

It is because of this pointer , although pointer is (integer) of 4 byte but it refer to a one memory location ( one Unit ) which is 1 byte.

Narayan das khatri