HP C Programmer's Guide (92434-90009)

22 Chapter2
Storage and Alignment Comparisons
Alignment Rules
d member char 0x7 @ 0x0
e member char 0x8 @ 0x0
The size of the structure is 12 bytes, with 4-byte alignment as illustrated in Figure 2-4. on
page 22.
Figure 2-4. Example of HPUX_NATURAL/HPUX_NATURAL_S500 Alignment for
Structure foo
Since b (being an int type) does not cross any word boundaries, a and b are adjacent. c
starts on the next word because it would cross a word boundary if it started right after b.
The zero length bit-field forces no further bit-field to be placed between the previous
bit-field, if any, and the next boundary described by the zero-length bit field's type. Thus, if
we are at bit 5 and see a zero length bit-field of type int, then the next member will start at
the next word boundary (bits 5-31 will be empty). However, if we are at bit 5 and see a zero
length bit-field of type char, then the next member will start at least at the next byte (bits
5-7 will be empty), depending on whether the next member can start at a byte-boundary.
DOMAIN_WORD/DOMAIN_NATURAL/NATURAL and NOPADDING Alignments
For DOMAIN_WORD, DOMAIN_NATURAL, NATURAL, and NOPADDING alignments:
All integral types are treated identically; that is, the packing for char a:17 (this is
legal) is the same as for int a:17.
Bit-fields can cross "natural" boundaries, unlike for HPUX_NATURAL. That is, for int
a:30; int b:7;, b will start at bit 30.
No bit-field can cross more than one 2-byte boundary. Thus, for int
a:14; int b:18;, b will start at bit 16. If it started at bit 14, it would illegally cross
both the 2- and 4-byte boundaries.
The use of any type and size of bit-field alone will only cause the entire structure to
have 2-byte alignment (1-byte for NOPADDING).
NOTE
NOPADDING of bit-fields follows the DOMAIN alignment scheme. This may result
in a full byte of padding between two bit-fields.
For example:
struct {
char c;
int i:31; < At offset 2 bytes.
} bar;
The above structure bar will align the bit-field at offset 2 bytes, so that there is a full byte