The data contained in Table 1 (in Using alignment modes) apply to scalar variables,
and variables that are members of aggregates such as structures, unions,
and classes. The following rules apply to aggregate
variables, namely structures, unions or classes, as a whole (in the
absence of any modifiers):
- For all alignment modes, the size of an aggregate is
the smallest multiple of its alignment value that can encompass all
of the members of the aggregate.
Empty aggregates are assigned a size of 0 bytes.
Empty aggregates are assigned a size of 1 byte.
Note that static data members do not participate in the alignment
or size of an aggregate; therefore a structure or class containing
only a single static data member has a size of 1 byte.
- For all alignment modes,
the alignment of an aggregate is equal to the largest alignment value
of any of its members. With the exception of packed alignment
modes, members whose natural alignment is smaller than that of their
aggregate's alignment are padded with empty bytes.
- Aligned aggregates can be nested, and the alignment rules applicable
to each nested aggregate are determined by the alignment mode that
is in effect when a nested aggregate is declared.
Notes:
The C++ compiler might generate extra fields for
classes that contain base classes or virtual functions. Objects of
these types might not conform to the usual mappings for aggregates.
- The alignment of an aggregate must be the same in all
compilation units. For example, if the declaration of an aggregate
is in a header file and you include that header file into two distinct
compilations units, choose the same alignment mode for both compilations
units.
For rules on the alignment of aggregates containing bit fields,
see Alignment of bit fields.