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.
- 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
- 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.
- push
- When specified without a number, pushes whatever value is currently in effect to the top of the packing "stack". When used with a number, pushes that value to the top of the packing stack, and sets the packing value to that of number for structures that follow.
- 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.
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 C
and C++
language
standards.


