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
That's really an implementation detail. Once long ago, I 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), sizeof
is defined as always returning one or greater, no matter what.
The size of a most derived class shall be greater than zero.
This is required for, among other things, allowing you to handle arrays of objects and pointers to them. If your elements were allowed to be zero-sized then &(array[0])
would be identical to &(array[42])
, which is going to cause all sorts of havoc to your processing loops.
The reason why it may not be a machine word is that there are no elements within it that actually 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 in that implementation).