Using alignment modifiers
XL C/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 or aggregate members. Available modifiers are as follows:
#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
- number: where number is 1, 2, 4, 8, or 16. That is, structure members are aligned on number-byte boundaries or on their natural alignment boundary, whichever is less.
nopack
: disables packing.pop
: removes the previous value added with#pragma pack
.- 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 it applies 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 two, 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 or class1. 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 might effectively increase 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.
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 the information about type attributes in the XL C/C++ Language Reference and the Cand C++
language standards.