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):
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.| Example | Size of aggregate | ||
|---|---|---|---|
| -qalign=power | -qalign=natural | -qalign=packed | |
| struct Struct1 { |
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 { |
15 bytes | 15 bytes | 15 bytes |
| struct Struct3 { |
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.) |
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. For rules on the alignment of aggregates containing bit fields, see Alignment of bit fields.