HP aC++/HP C A.06.25 Programmer's Guide

struct ST1 {
char c1;
T x;
char c2;
};
#pragma pack
ST1<int> obj; // Same layout as S2 in the prior example
template <> // Explicit specialization
struct ST1<void> {
char c1;
char c2;
}; // Undefined (unsupported) behavior
// ST1 was defined under a #pragma pack 1 directive
NOTE: The alignment of specializations and partial specializations of templates is
undefined and unsupported if either the primary template or the specialization is under
the influence of a #pragma pack directive.
Handling Unaligned Data
Direct access to unaligned class fields is handled automatically by HP aC++. However,
this results in slower access times than for aligned data. Indirect access (through pointers
and references) to unaligned class fields is also handled automatically.
If you take the address of a data field and assign it to a pointer, it is not handled
automatically and is likely to result in premature termination of the program if not
handled appropriately.
Example:
#include <stdio.h>
#pragma pack 1
struct S1 {
char c1;
int i;
char c2;
};
#pragma pack
int main() {
S1 s;
S1 *p = &s;
printf(%d\n, s.i); // OK
printf(%d\n, p->i); // OK
int *ip = &p->i; // Undefined behavior
// Likely Abort unless compiled with +u1
// The address of a reference (*ip) is
// assigned to an int pointer.
printf(%d\n, *ip);
}
Data Alignment Pragmas 133