Using alignment modes and modifiers

Within aggregates that contain different data types, including C and C++ structures and unions, and C++ classes, each data type supported by XL C/C++ is aligned along byte boundaries according to platform-specific defaults, as follows:

Each of these settings is defined in Table 6.

You can also explicitly control the alignment of data by using an alignment mode, as well as alignment modifiers. Alignment modes allow you to do the following:

Set the alignment for all aggregates in a single file or multiple files in the compilation process
To use this approach, you specify the -qalign compiler option during compilation. The valid suboptions for -qalign for each platform are provided in Table 6.

Set the alignment for a single aggregate or multiple aggregates in a file
To use this approach, you specify the #pragma align or #pragma options align directives in the source files. The valid suboptions for #pragma align for each platform are provided in Table 6. Each directive changes the alignment rule in effect for all aggregates that follow the directive until another directive is encountered, or until the end of the compilation unit.

Set the alignment for a single aggregate
In addition to the #pragma align directive, you can use the following in source files:

Alignment modifiers allow you to do the following:

Set the alignment for all members in an aggregate
To use this approach, you can use any of the following in source files:

Set the alignment for a single member within an aggregate
To use this approach, include __attribute__((packed)) or __attribute__((aligned(n))) type or variable attributes in structure declarations. The value for n in must be a positive power of 2. For more information on the variable attributes, see "The aligned Variable Attribute" and "The packed Variable Attribute" in XL C/C++ Language Reference. For information on the type attributes, see "Type Attributes" in XL C/C++ Language Reference.
Note:
The __align specifier and __attribute__((aligned)) attribute do not change the alignment of vector types.

Table 6. Alignment settings

Data type Storage Alignment settings and supported platforms
natural power full mac68k twobyte linuxppc bit_packed packed
AIX
Mac
AIX
Mac
AIX AIX
Mac
AIX Linux AIX
Mac
Linux
AIX
_Bool (C), bool (C++), _Bool 1 byte n/a n/a n/a 1 byte 1 byte n/a
char, signed char, unsigned char 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte
wchar_t (32-bit mode) 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 1 byte
wchar_t (64-bit mode) 4 bytes 4 bytes 4 bytes not supported2 4 bytes 1 byte
int, unsigned int 4 bytes 4 bytes 4 bytes 2 bytes 4 bytes 1 byte
short int, unsigned short int 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 1 byte
long int, unsigned long int (32-bit mode) 4 bytes 4 bytes 4 bytes 2 bytes 4 bytes 1 byte
long int, unsigned long int (64-bit mode) 8 bytes 8 bytes 8 bytes not supported2 8 bytes 1 byte
long long 8 bytes 8 bytes 8 bytes 2 bytes 8 bytes 1 byte
float 4 bytes 4 bytes 4 bytes 2 bytes 4 bytes 1 byte
double 8 bytes 8 bytes see note1 2 bytes 8 bytes 1 byte
long double 8 bytes 8 bytes see note1 2 bytes 8 bytes 1 byte
pointer (32-bit mode) 4 bytes 4 bytes 4 bytes 2 bytes 4 bytes 1 byte
pointer (64-bit mode) 8 bytes 8 bytes 8 bytes not supported2 8 bytes 1 byte
vector types3 16 bytes 16 bytes 16 bytes n/a 16 bytes n/a 16 bytes 1 byte n/a

Notes:

  1. These types use the natural alignment for the first member in the aggregate and 4 bytes or the natural alignment (whichever is less) for subsequent members.

  2. If you declare an aggregate with a member of this type and try to compile with this alignment setting, the compiler issues a warning message, and compiles with the default alignment setting for the appropriate platform.

  3. Only supported when the -qaltivec compiler option is in effect.

If you generate data with an application on one platform and read the data with an application on another platform, you will want to be sure to use a platform-neutral alignment mode, such as #pragma pack or qalign=bit_packed.

Note:
C++ The C++ compiler might generate extra fields for classes that contain base classes or virtual functions. Objects of these types might not conform to the usual mappings for aggregates.

General rules for alignment

If you control the alignment of aggregates with any of the settings listed in Table 6, the following rules apply:

For rules on aligning aggregates containing bit fields, see Using and aligning bit fields. IBM Copyright 2003