The aligned variable attribute

The aligned variable attribute allows you to override the default alignment mode to specify a minimum alignment value, expressed as a number of bytes, for any of the following:

The attribute is typically used to increase the alignment of the given variable.

Read syntax diagramSkip visual syntax diagramaligned 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 1048576 bytes. 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 attribute specification is ignored, and the compiler simply uses the default alignment in effect.

When you apply the aligned attribute to a bit field structure member variable, 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.

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  ;

Related information