I know the size of an empty class is defined by the standard to be non-zero. It is usually 1 byte on most implementations.
But, does the C++ standard specify the minimum possible size of an object? Is it logical to assume that as per standard the size of an empty class object will be atleast 1 byte.
Asked
Active
Viewed 168 times
2

Alok Save
- 202,538
- 53
- 430
- 533
1 Answers
8
The minimum size of an object is zero (§1.8/5). However, complete objects must always have non-zero size, and that size must be at least one.
Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size. An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage.
Base class subobjects of empty types can have zero size thanks to what is known as EBCO, the empty base class optimization.

R. Martinho Fernandes
- 228,013
- 71
- 433
- 510
-
1It says _subobjects_. Does that count? I think the key is that a most-derived object shall have _non-zero_ size, which means any complete object must be at least 1 byte wide. – Lightness Races in Orbit Feb 11 '13 at 12:50
-
So `sizeof (object.subobject)` can be 0 if `subobject` is an empty class instance? – leemes Feb 11 '13 at 12:50
-
@leemes: I believe you're confusing "subobject" with "member" – Lightness Races in Orbit Feb 11 '13 at 12:51
-
1@Lightness subobjects are still objects. The question does not restrict itself to most-derived objects. – R. Martinho Fernandes Feb 11 '13 at 12:52
-
@LightnessRacesinOrbit You're right. In this case: `sizeof(*(EmptySubclass*)this)` => is non-zero, isn't it? How can we treat subobjects as "objects" (instances of the base class)? – leemes Feb 11 '13 at 12:52
-
@R.MartinhoFernandes: Are they? I think that subobjects do not count as objects in the same sense. – Lightness Races in Orbit Feb 11 '13 at 12:52
-
@leemes no. `sizeof` always gives you the size of the type. – R. Martinho Fernandes Feb 11 '13 at 12:52
-
1@LightnessRacesinOrbit same clause, paragraph 2 "Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element. An object that is not a subobject of any other object is called a complete object." – R. Martinho Fernandes Feb 11 '13 at 12:53
-
The standard does appear to call subobjects "objects" and treat them similarly. Still, I'd have thought the intent of the OP was to ask about complete objects. – Lightness Races in Orbit Feb 11 '13 at 12:54
-
Well, in that case the same quote still answers it. Only my fluff may or may not be relevant :P – R. Martinho Fernandes Feb 11 '13 at 12:54
-
Well, make the distinction clearer to the OP and you can have an upvote. – Lightness Races in Orbit Feb 11 '13 at 12:55
-
Thanks. I was meaning to ask about *complete objects* and was looking for the specific *"shall occupy one or more bytes of storage."*. – Alok Save Feb 11 '13 at 14:21