Alignment of aggregates
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 zero bytes. As a result, two distinct variables
might have the same address.
Empty aggregates are assigned a size of one 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 one byte.- For all alignment modes except mac68k, 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.
- For mac68k alignment, if the aggregate does not contain a vector member, the alignment is 2 bytes. If an aggregate contains a vector member, then the alignment is the largest alignment of all of its members.
- 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.
The following table shows some examples of the size of an aggregate according to
alignment mode.
| Example | Size of aggregate | ||
|---|---|---|---|
| #pragma align (power) | #pragma align (natural) | #pragma align (packed) | |
|
struct Struct1 {
double a1; char a2; }; |
16 bytes (The member with the largest alignment requirement is
a1; therefore, a2 is padded with 7 bytes.) |
16 bytes (The member with the largest alignment requirement is
a1; therefore, a2 is padded with 7 bytes.) |
9 bytes (Each member is packed to its natural alignment; no padding is added.) |
|
struct Struct2 {
char buf[15]; }; |
15 bytes | 15 bytes | 15 bytes |
|
struct Struct3 {
char c1; double c2; }; |
12 bytes (The member with the largest alignment requirement is
c2; however, because it is a double and is not the first member,
the 4-byte alignment rule applies. c1 is padded with 3 bytes.) |
16 bytes (The member with the largest alignment requirement is
c2; therefore, c1 is padded with 7 bytes.) |
9 bytes (Each member is packed to its natural alignment; no padding is added.) |
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.