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 members 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, and empty brackets. The use of empty brackets
has the same functionality as nopack.
- __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:
- 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.
- 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:
- simple, or scalar, variables, including members of aggregates (structures,
unions or classes) and user-defined types created by typedef statements.
- aggregate variables (structures, unions or classes)