Caveats
12-13
12
However, #pragma align has limitations. Although it can be used to
restrict the padding of aggregate data types (and arrays of those types) it
does not change the alignment rules for individual structure members. For
information on alignment rules for structure members, see the discussion
of
pragma pack in this manual.
Consider the following example:
struct test {
char first;
int second;
short third;
};
If you compiled the above structure without modification, the structure
size would be 16 bytes. If you defined
pragma align 1 before the
structure definition, the structure size would be 12 bytes - four pad bytes
removed. In both cases, however, the position of the elements would not
have changed, with element "first" at address offset zero, element
"second" at address offset 4, and element "third" at address offset 8. This
element placement effectively creates three pad bytes between the first
and second structure elements.
To work around the limitations of intra-structure padding, consider the
case where the above structure must be read in from a binary file written
by a processor/tool pair that inserted zero (intra-struct) pad bytes.
The following code demonstrates one way to perform that function:
#include <unalign.h>
/* The following structure is what gcc960 compiles.
* The buffer, when filled, contains the same
* structure in packed format - all pad bytes removed. */
struct test {
char first;
int second;
short third;
} 960_struct;
unsigned char packed[7];
/* sum of 960_struct element sizes */