The aligned type attribute

The aligned type attribute allows you to override the default alignment mode to specify a minimum alignment value, expressed as a number of bytes, for a structure, class, union, enumeration, or other user-defined type created in a typedef declaration. The aligned attribute is typically used to increase the alignment of any variables declared of the type to which the attribute applies.

Read syntax diagramSkip visual syntax diagramaligned type attribute syntax
       '-__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 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.

The alignment value that you specify will be applied to all instances of the type. Also, the alignment value applies to the variable as a whole; if the variable is an aggregate, the alignment value applies to the aggregate as a whole, not to the individual members of the aggregate.

In all of the following examples, the aligned attribute is applied to the structure type A. Because a is declared as a variable of type A, it will also receive the alignment specification, as will any other instances declared of type A.

struct __attribute__((__aligned__(8))) A {};

struct __attribute__((__aligned__(8))) A {} a;

typedef struct __attribute__((__aligned__(8))) A {} a;

Related information