Language Implementation
7-7
7
Union types align according to the greatest alignment
requirement of any member of the union. In the
example below,
un1 aligns on a 4-byte boundary
since the alignment of
c, the largest element,
is 4:
union un1 {
short a; /* 2 bytes */
char b; /* 1 byte */
int c; /* 4 bytes */
};
Structure types align according to the alignment of the member
types either natural or user-constrained.
Specifying optimal or backward-compatible natural alignment changes the
size of a structure. Natural alignments differ only in tail padding.
Member offsets, and therefore the padding between members, are the
same under optimal natural alignment as under backward-compatible
natural alignment. For example, the following structure occupies memory
as shown in Figure 7-1 under either optimal or backward-compatible
natural alignment:
struct strc1
{
char a; /* occupies byte 0 */
short b; /* occupies bytes 2 and 3 */
char c; /* occupies byte 4 */
int d; /* occupies bytes 8 through 11 */
};
Under optimal natural alignment, the size and alignment of the struct
type are both 16. Under backward-compatible natural alignment, the size
is 12 and the alignment is 4.