HP C Programmer's Guide (92434-90009)

Chapter 2 27
Storage and Alignment Comparisons
The HP_ALIGN Pragma
Using the HP_ALIGN Pragma
The HP_ALIGN pragma allows you to control data storage allocation and alignment of
structures, unions, and typedefs.
NOTE
The basic scalar types, array types, enumeration types, and pointer types are
not
affected by the HP_ALIGN pragma. The pragma only affects struct or
union types and typedefs - No other types are affected by specifying the
HP_ALIGN pragma.
The HP_ALIGN pragma takes a parameter that specifies the alignment mode for example:
#pragma HP_ALIGN HPUX_NATURAL
There is also an optional parameter PUSH, which saves the current alignment mode before
setting the specified mode as the new alignment mode. For example, in the following
sequence:
#pragma HP_ALIGN NOPADDING PUSH
/* decls following */
the current alignment mode is saved on the stack. It is then set to the new alignment
mode, NOPADDING.
The PUSHed alignment mode can be retrieved later by doing a
#pragma HP_ALIGN POP
If the last alignment mode PUSHed on the stack was NOPADDING, the current alignment
mode would now be NOPADDING.
Problems Sometimes Encountered with the HP_ALIGN Pragma
If only one alignment mode is used throughout the entire file, this pragma is
straightforward to use and to understand. However, whena different mode isintroduced in
the middle of the file, you should be aware of its implications and effects.
The key to understanding HP_ALIGN is the following concept: typedefs and struct or union
types retain their original alignment mode throughout the entire file. Therefore, when a
type with one alignment is used in a different alignment mode, it will still keep its original
alignment.
This feature may lead to confusion when you have a typedef, structure or union of one
alignment nested inside a typedef, structure or union of another alignment.
Here are some examples of the most common misunderstandings.
Example 1: Using Typedefs The alignment pragma will affect typedef, struct, and
union types. Therefore, in the following declaration:
#pragma HP_ALIGN HPUX_WORD
typedef int int32;
int32 is not equivalent to int. To illustrate:
#pragma HP_ALIGN HPUX_WORD