HP C Programmer's Guide (92434-90009)

32 Chapter2
Storage and Alignment Comparisons
The HP_ALIGN Pragma
typedef int non_native_int;
#pragma HP_ALIGN POP
main ()
{
int i;
non_native_int *p = non_native_rec.b;
i = *p;
}
An alternative to using the HP_ALIGN pragma and typedefs to control non-natively aligned
pointers is to use the +u
bytes
compiler option of HP C/HP-UX. The +u
bytes
forces all
pointer dereferences to assume that data is aligned on 8-bit, 16-bit, or 32-bit addresses.
The value of
bytes
can be 1 (8-bit), 2 (16-bit), or 4 (32-bit). This option can be used when
accessing non-natively aligned data with pointers that would otherwise be natively
aligned. This option can be useful with code that generates the compiler warning message
#565 - "address operator applied to non natively aligned member."
and aborts with a run-time error.
The +u
bytes
option affects all pointer dereferences within the source file. It can have a
noticeable, negative impact on performance.
NOTE
The HP C/iX implementation of the +u option omits the
bytes
parameter.
Defining Platform Independent Data Structures
One way to avoid trouble caused by differences in data alignment is to define structures so
they are aligned the same on different systems. To do this, use padding bytes — that is,
dummy variables to align fields the same way on different architectures.
For example, use:
struct {
char cl;
char dum1;
char dum2;
char dum3;
int i1;
};
instead of:
struct {
char c1;
int i1;
};