Using alignment modifiers

XL C/C++ also provides alignment modifiers, which allow you to exercise even finer-grained control over alignment, at the level of declaration or definition of individual variables. Available modifiers are:

#pragma pack(...)
Valid application:
The entire aggregate (as a whole) immediately following the directive.
Effect:
Sets the maximum alignment of the aggregate to which it applies, to a specific number of bytes. Also allows a bit-field to cross a container boundary. Used to reduce the effective alignment of the selected aggregate.
Valid values:
1, 2, 4, 8, 16, nopack, pop. Empty brackets are not valid.
__attribute__((aligned(n)))
Valid application:
As a variable attribute, it applies to a single aggregate (as a whole), namely a structure, union, or class; or to an individual member of an aggregate.1 As a type attribute, it applies to all aggregates declared of that type. If it is applied to a typedef declaration, it applies to all instances of that type.2
Effect:
Sets the minimum alignment of the specified variable (or variables), to a specific number of bytes. Typically used to increase the effective alignment of the selected variables.
Valid values:
n must be a positive power of 2, or NIL. NIL can be specified as either __attribute__((aligned())) or __attribute__((aligned)); this is the same as specifying the maximum system alignment (16 bytes on all UNIX platforms). .
__attribute__((packed))
Valid application:
As a variable attribute, it applies to simple variables, or individual members of an aggregate, namely a structure, union or class.1 As a type attribute, it applies to all members of all aggregates declared of that type.
Effect:
Sets the maximum alignment of the selected variable, or variables, to which it applies, to the smallest possible alignment value, namely one byte for a variable and one bit for a bit field.
__align(n)
Effect:
Sets the minimum alignment of the variable or aggregate to which it applies to a specific number of bytes; also effectively increases the amount of storage occupied by the variable. Used to increase the effective alignment of the selected variables.
Valid application:
Applies to simple static (or global) variables or to aggregates as a whole, rather than to individual members of aggregates, unless these are also aggregates.
Valid values:
n must be a positive power of 2. XL C/C++ also allows you to specify a value greater than the system maximum, up to an absolute maximum of .
Notes:
  1. In a comma-separated list of variables in a declaration, if the modifier is placed at the beginning of the declaration, it applies to all the variables in the declaration. Otherwise, it applies only to the variable immediately preceding it.
  2. Depending on the placement of the modifier in the declaration of a struct, it can apply to the definition of the type, and hence applies to all instances of that type; or it can apply to only a single instance of the type. For details, see "Type Attributes" .

When you use alignment modifiers, the interactions between modifiers and modes, and between multiple modifiers, can become complex. The following sections outline the precedence rules for alignment modifiers, for the following types of variables:

Precedence rules for scalar variables

The following formulas use a "top-down" approach to determining the alignment, given the presence of alignment modifiers, for both non-embedded (standalone) scalar variables and embedded scalars (variables declared as members of an aggregate):

Alignment of variable = maximum(effective type alignment , modified alignment value)

where effective type alignment = maximum(maximum(aligned type attribute value, __align specifier value) , minimum(type alignment, packed type attribute value))

and modified alignment value = maximum(aligned variable attribute value, packed variable attribute value)

and where type alignment is the alignment mode currently in effect when the variable is declared, or the alignment value applied to a type in a typedef statement.

In addition, for embedded variables, which can be modified by the #pragma pack directive, the following rule applies:

Alignment of variable = minimum(#pragma pack value , maximum(effective type alignment , modified alignment value))

Note:
If a type attribute and a variable attribute of the same kind are both specified in a declaration, the second attribute is ignored.

Precedence rules for aggregate variables

The following formulas determine the alignment for aggregate variables, namely structures, unions, and classes:

Alignment of variable = maximum(effective type alignment , modified alignment value)

where effective type alignment = maximum(maximum(aligned type attribute value, __align specifier value) , minimum(aggregate type alignment, packed type attribute value))

and modified alignment value = maximum (aligned variable attribute value , packed variable attribute value)

and where aggregate type alignment = maximum (alignment of all members )

Note:
If a type attribute and a variable attribute of the same kind are both specified in a declaration, the second attribute is ignored.