HP C A.06.05 Reference Manual
Compiling and Running HP C Programs
Pragmas
Chapter 9 229
The PACK pragma is not intended to be an “extension” of the HP_ALIGN pragma. It is, instead,
a simple and highly portable way of controlling the alignment of aggregates. It has some
significant differences with the HP_ALIGN pragma, including uniform bitfield alignment,
uniform struct and union alignment, and the lack of PUSH and POP functionality.
For complete details on the use of HP_ALIGN and PACK pragmas, refer to Chapter 2, “Storage
and Alignment Comparisons,” in the HP C/HP-UX Programmer's Guide.
UNALIGN Pragma
#pragma unalign [1|2|4|8|16]
typedef T1 T2;
T1 and T2 have the same size and layout, but with specified alignment requirements.
HP C supports misaligned data access using the unalign pragma. The unalign pragma can
be applied on typedef to define a type with special alignment. The unalign pragma takes effect
only on next declaration.
If the unalign pragma declaration is not in the global scope or if it is not a typedef, compiler
displays a warning message. If the specified alignment is greater than the original alignment
of the declaration, then an error message is displayed, and the pragma is ignored. For
example,
#pragma unalign 1
typedef int ua_int; // ua_int is of int type
// with 1 byte alignment
typedef ua_int *ua_intPtr; // this typedef is not affected
// affected by the above unalign
// pragma. it defines a pointer
// type which points to 1 byte
// aligned int
The interaction between pack and unalign pragmas is as follows:
#pragma pack 1
struct S {
char c;
int i;
};
#pragma pack 0
S s;
ua_int *ua_ip = &s.i; // ua_ip points to 1 byte aligned int
*ua_ip = 2; // mis-aligned access to 1 byte aligned int