If you will create an object of a totally empty class it's size will be >0 as guaranteed by the standard (in order two objects have different addresses). But I'm interested what is stored in this byte (some value, index, address)? Is there some way to know this?
-
4First of all who said that it is guaranteed to be exactly 1 byte? – Slava Feb 25 '13 at 18:05
-
It isn't defined. Ergo, you shouldn't temper with it. – StoryTeller - Unslander Monica Feb 25 '13 at 18:07
-
@Slava Ok.Thanks for the tip. Actually the size must be greater than zero. But the questuion still remains – MainstreamDeveloper00 Feb 25 '13 at 18:08
-
@slava my thoughts exactly, although sizeof(Bogus) does return 1 using my version of g++ for whatever that's worth... I would have assumed it was sizeof(void *) otherwise – Foon Feb 25 '13 at 18:08
-
@HarryCater your next question will be what integer is passed to an overloaded postfix operator++? Is that value different than operator--? – Slava Feb 25 '13 at 18:10
-
If you inherit from it. Then the size can become zero so your assumption of greater than zero does not hold. – Martin York Feb 25 '13 at 18:54
4 Answers
Technically, What is stored there is Unspecified.
In short it is left up to the implementation and the implementation does not need to document the behavior.

- 202,538
- 53
- 430
- 533
It has the same (unknown, undefined) content as any other filler that is used to fill gaps between for example a char
and an int
member. And in common with the "gap-filler", it has no purpose other than as a "make sure the placement of this data works out". If the size of an empty class was zero, an array of two such objects would take up zero bytes and both objects would have the same address, which would make things rather strange for all manner of things.

- 126,704
- 14
- 140
- 227
If i recall correctly the standard guarantees that an object of an empty class will occupy space. It doesn't say exactly how much space, neither what is stored there, so that is up to the implementer.
If you want to know what is in the given space, you could just reinterpret cast it and have a look.

- 6,115
- 1
- 31
- 50
Nothing, it is just padding. You cannot depend on the value stored there, or even that there is any value at all, just like with any other padding.

- 204,818
- 23
- 294
- 489