The aligned variable attribute

With the aligned variable attribute, you can override the default memory alignment mode to specify a minimum memory alignment value, expressed as a number of bytes, for any of the following types of variables:
  • Non-aggregate variables
  • Aggregate variables (such as a structures, classes, or unions)
  • Selected member variables
The attribute is typically used to increase the alignment of the given variable.
Read syntax diagramSkip visual syntax diagram
aligned variable attribute syntax

>>-__attribute__------------------------------------------------>

>--((--+-aligned-----+--+------------------------+--))---------><
       '-__aligned__-'  '-(--alignment_factor--)-'       

The alignment_factor is the number of bytes, specified as a constant expression that evaluates to a positive power of 2. You can specify a value up to a maximum of 268435456. If you omit the alignment factor, and its enclosing parentheses, the compiler automatically uses 16 bytes. If you specify an alignment factor greater than the maximum, the compiler issues an error message and the compilation fails.

When you apply the aligned attribute to a member variable in a bit field structure, the attribute specification is applied to the bit field container. If the default alignment of the container is greater than the alignment factor, the default alignment is used.

Example

In the following example, the structures first_address and second_address are set to an alignment of 16 bytes:
struct address {
                 int street_no;
                 char *street_name;
                 char *city;
                 char *prov;
                 char *postal_code;
               } first_address __attribute__((__aligned__(16))) ;

struct address second_address __attribute__((__aligned__(16))) ;
In the following example, only the members first_address.prov and first_address.postal_code are set to an alignment of 16 bytes:
struct address {
                 int street_no;
                 char *street_name;
                 char *city;
                 char *prov __attribute__((__aligned__(16))) ;
                 char *postal_code __attribute__((__aligned__(16))) ;
               } first_address  ;


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us