HP C Programmer's Guide (92434-90009)
28 Chapter2
Storage and Alignment Comparisons
The HP_ALIGN Pragma
typedef int int32;
void routine (int *x);
int main()
{
int *ok;
int32 *bad;
routine(ok);
routine(bad); /* warning */
}
Compiling this with -Aa -c will give two warnings:
warning 604: Pointers are not assignment-compatible.
warning 563: Argument #1 is not the correct type.
These warnings occur because the actual pointer value of bad may not be as strictly
aligned as the pointer type routine expects. This may lead to run-time bus errors in the
called function if it dereferences the misaligned pointer.
Example 2: Using Combination of Different Alignment Modes In the WORD
alignment modes, the members of a structure whose sizes are larger than 2 bytes are
aligned on a 2-byte boundary. However, this is only true if those member types are scalar
or have been previously declared under the same alignment mode. If the member type is a
typedef, struct, or union type which has been declared previously under a different
alignment mode, it will retain its original alignment,regardless of current alignmentmode
in effect. For example:
typedef int my_int;
#pragma HP_ALIGN HPUX_WORD
struct st {
char c;
my_int i;
};
int main()
{
char c;
struct st foo;
}
Although the size of my_int is greater than 2 bytes, because it was declared previously
under HPUX_NATURAL with the alignment of 4 bytes it will be aligned on a 4-byte boundary,
causing the entire struct st to be aligned on a 4-byte boundary. Compiling with the +m
option to show the offsets of the identiļ¬ers (offsets given as "byte-offset @ bit-offset" in
hexadecimal), you will get the following output:
main
Identifier Class Type Address
-- -