Alok Save
Alok Save

Reputation: 206528

Smallest standard compliant object size in C++?

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.

Upvotes: 2

Views: 198

Answers (1)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234434

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.

Upvotes: 8

Related Questions