HP C Programmer's Guide (92434-90009)
Chapter 5 127
Programming for Portability
General Portability Considerations
Checking for Alignment Problems with lint
If invoked with the -s option, the lint command generates warnings for C constructs that
may cause portability and alignment problems between Series 300/400 and Series 700/800,
and vice versa. Specifically, lint checks for these cases:
• Internal padding of structures. lint checks for instances where a structure member
may be aligned on a boundary that is inappropriate according to the most-restrictive
alignment rules. For example, given the code
struct s1 { char c; long l; };
lint issues the warning:
warning: alignment of struct 's1' may not be portable
• Alignment of structures and simple types. For example, in the following code, the
nested struct would align on a 2-byte boundary on Series 300/400 and an 8-byte
boundary on Series 700/800:
struct s3 { int i; struct { double d; } s; };
In this case, lint issues this warning about alignment:
warning: alignment of struct 's3' may not be portable
• End padding of structures. Structures are padded to the alignment of the
most-restrictive member. For example, the following code would pad to a 2-byte
boundary on Series 300/400 and a 4-byte boundary for Series 700/800:
struct s2 { int i; short s; };
In this case, lint issues the warning:
warning: trailing padding of struct/union 's2' may not be portable
Note that these are only potential alignment problems. They would cause problems only
when a program writes raw files which are read by another system. This is why the
capability is accessible only through a command line option; it can be switched on and off.
lint does not check the layout of bit-fields.
Ensuring Alignment without Pragmas
Another solution to alignment differences between systems would be to define structures
in such a way that they are forced into the same layout on different systems. To do this, use
padding bytes — that is, dummy variables that are inserted solely for the purpose of
forcing struct layout to be uniform across implementations. For example, suppose you
need a structure with the following definition:
struct S {
char c1;
int i;
char c2;
double d;
};
An alternate definition of this structure that uses filler bytes to ensure the same layout on