Using alignment modes

Each data type that is supported by XL C/C++ is aligned along byte boundaries according to platform-specific default alignment modes. On AIX®, the default alignment mode is power or full, which are equivalent.

XL-based front end begins
You can change the default alignment mode as follows:
  • Set the alignment mode for all variables in a single file or multiple files during compilation.

    To use this approach, you specify the -qalign compiler option during compilation, with one of the suboptions listed in Table 1.

  • Set the alignment mode for all variables in a section of source code.

    To use this approach, you specify the #pragma align or #pragma options align directives in the source files, with one of the suboptions listed in Table 1. Each directive changes the alignment mode in effect for all variables that follow the directive until another directive is encountered, or until the end of the compilation unit.

XL-based front end ends
Clang-based front end begins
You can change the default alignment mode as follows:
  • Set the alignment mode for all variables in a section of source code.

    To use this approach, you specify the #pragma align directives in the source files, with one of the suboptions listed in Table 1. Each directive changes the alignment mode in effect for all variables that follow the directive until another directive is encountered, or until the end of the compilation unit.

Clang-based front end ends

Each of the valid alignment modes is defined in Table 1, which provides the alignment value, in bytes, for scalar variables of all data types. Where there are differences between 32-bit and 64-bit modes, these are indicated. Also, where there are differences between the first (scalar) member of an aggregate and subsequent members of the aggregate, these are indicated.

Table 1. Alignment settings (values given in bytes)
Data type Storage Alignment setting
natural
XL-based front end begins
power, full

Clang-based front end begins
power
XL-based front end begins
mac68k, twobyte3
XL-based front end begins
bit_packed2
packed2
_Bool (C), bool (C++) (32-bit mode) 1 1 1 1 1 1
_Bool (C), bool (C++) (64-bit mode) 1 1 1 not supported 1 1
char, signed char, unsigned char 1 1 1 1 1 1
wchar_t (32-bit mode) 2 2 2 2 1 1
wchar_t (64-bit mode) 4 4 4 not supported 1 1
int, unsigned int 4 4 4 2 1 1
short int, unsigned short int 2 2 2 2 1 1
long int, unsigned long int (32-bit mode) 4 4 4 2 1 1
long int, unsigned long int (64-bit mode) 8 8 8 not supported 1 1
XL-based front end begins_Decimal32 4 4 4 2 1 1
XL-based front end begins_Decimal64 8 8 8 2 1 1
XL-based front end begins_Decimal128 16 16 16 2 1 1
long long 8 8 8 2 1 1
float 4 4 4 2 1 1
double 8 8 see note1 2 1 1
long double 8 8 see note1 2 1 1
XL-based front end beginslong double with -qldbl128 16 16 see note1 2 1 1
pointer (32-bit mode) 4 4 4 2 1 1
pointer (64-bit mode) 8 8 8 not supported 1 1
vector types 16 16 16 16 1 1
Notes:
  1. In aggregates, the first member of this data type is aligned according to its natural alignment value; subsequent members of the aggregate are aligned on 4-byte boundaries.
  2. The packed alignment will not pack bit-field members at the bit level; use the bit_packed alignment if you want to pack bit fields at the bit level.
  3. For mac68k alignment, if the aggregate does not contain a vector member, the alignment is 2 bytes. If an aggregate contains a vector member, then the alignment is the largest alignment of all of its members.
If you are working with aggregates containing double, long long, or long double data types, use the natural mode for highest performance, as each member of the aggregate is aligned according to its natural alignment value. XL-based front end beginsIf you generate data with an application on one platform and read the data with an application on another platform, it is recommended that you use the bit_packed mode, which results in equivalent data alignment on all platforms.XL-based front end ends
Notes:
  • Vectors in a bit-packed structure might not be correctly aligned unless you take extra action to ensure their alignment.
  • Vectors might suffer from alignment issues if they are accessed through heap-allocated storage or through pointer arithmetic. For example, double my_array[1000] __attribute__((__aligned__(16))) is 16-byte aligned while my_array[1] is not. How my_array[i] is aligned is determined by the value of i.

Alignment of aggregates discusses the rules for the alignment of entire aggregates and provides examples of aggregate layouts. Alignment of bit-fields discusses additional rules and considerations for the use and alignment of bit fields and provides an example of bit-packed alignment.