HP C Programmer's Guide (92434-90009)

24 Chapter2
Storage and Alignment Comparisons
Alignment Rules
Figure 2-6. Example of Structures basic_str and enum_str
Notice that char_bit follows the HPUX_NATURAL alignment scheme, but char_enum_bit
follows the DOMAIN_WORD alignment scheme, even though the length of their bit-field types
are equivalent.
Alignment of Typedefs
Alignment for typedefs is slightly different than alignment for structures. Within a
structure, the member itself is affected by the alignment mode. However, with a typedef,
the alignment of the type that the typedef name is derived from is affected, not the typedef
name itself. The typedef name is then associated with the derived type.
When a typedef is seen, a new type is created by:
1. Taking the innermost type from which the typedef name is derived (which may be
another derived type).
2. Setting its alignment to what it would be if it were used inside a structure or union
declaration.
3. Creating a derived type from that new type, associating it with the typedef name.
Let us start with a simple example of a declaration under NOPADDING:
typedef int my_int;
Since an int will be 1-byte aligned inside a structure under NOPADDING, my_int will be
1-byte aligned.
Consider a pointer typedef with NOPADDING alignment:
typedef int **my_double_ptr;
my_double_ptr is derived from an integer type; therefore, a new integer type of 1-byte
alignment is created. my_double_ptr is defined to be a 4-byte aligned pointer to another
4-byte aligned pointer which points to a byte-aligned int.
Consider another example, this time with HPUX_WORD:
typedef int *my_ptr;
typedef my_ptr *my_double_ptr;