XL C also
provides alignment
modifiers, with which you can 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. Note: on AIX® #pragma
pack does not apply to bit-field union members.
- 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:
- n: where n is
1, 2, 4, 8, or 16. That is, structure members are aligned on n-byte
boundaries or on their natural alignment boundary, whichever is less. nopack: disables
packing. pop: removes the previous value added with #pragma
pack. Note: empty brackets has the same functionality as pop.
- __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 also
allows you to specify a value greater than the system maximum.
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 in the XL C Language
Reference.
When you use alignment modifiers, the interactions between modifiers
and modes, and between multiple modifiers, can become complex. The
following sections outline precedence guidelines 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)