The __align type qualifier (IBM extension)

The __align qualifier is a language extension that allows you to specify an explicit alignment for an aggregate or a static (or global) variable. The specified byte boundary affects the alignment of an aggregate as a whole, not that of its members. The __align qualifier can be applied to an aggregate definition nested within another aggregate definition, but not to individual elements of an aggregate. The alignment specification is ignored for parameters and automatic variables.

A declaration takes one of the following forms:

Read syntax diagramSkip visual syntax diagram
__align qualifier syntax for simple variables

>>-type specifier--__align--(--int_constant--)--declarator-----><

Read syntax diagramSkip visual syntax diagram
__align qualifier syntax for structures or unions

>>-__align--(--int_constant--)--+-struct-+---------------------->
                                '-union--'   

>--+----------------+--{--member_declaration_list--}--;--------><
   '-tag_identifier-'                                     

where int_constant is a positive integer value indicating the byte-alignment boundary. Legal values are powers of 2 up to 32768.
The following restrictions and limitations apply:
  • The __align qualifier cannot be used where the size of the variable alignment is smaller than the size of the type alignment.
  • Not all alignments may be representable in an object file.
  • The __align qualifier cannot be applied to the following:
    • Individual elements within an aggregate definition.
    • Individual elements of an array.
    • Variables of incomplete type.
    • Aggregates declared but not defined.
    • Other types of declarations or definitions, such as a typedef, a function, or an enumeration.

Examples using the __align qualifier

Applying __align to static or global variables:

// varA is aligned on a 1024-byte boundary and padded with 1020 bytes
int __align(1024) varA;

int main()                     
{...}                                 

// varB is aligned on a 512-byte boundary and padded with 508 bytes
static int __align(512) varB;

// Error
int __align(128) functionB( );

// Error
typedef int __align(128) T;

// Error
__align enum C {a, b, c};

Applying __align to align and pad aggregate tags without affecting aggregate members:

// Struct structA is aligned on a 1024-byte boundary
// with size including padding of 1024 bytes.
__align(1024) struct structA 
{
   int i; 
   int j;
};


// Union unionA is aligned on a 1024-byte boundary
// with size including padding of 1024 bytes.
__align(1024) union unionA 
{
   int i; 
   int j;
};

Applying __align to a structure or union, where the size and alignment of the aggregate using the structure or union is affected:

// sizeof(struct S) == 128
__align(128) struct S {int i;}; 

// sarray is aligned on 128-byte boundary with sizeof(sarray) == 1280
struct S sarray[10];                

// Error: alignment of variable is smaller than alignment of type 
struct S __align(64) svar;          

// s2 is aligned on 128-byte boundary with sizeof(s2) == 256
struct S2 {struct S s1; int a;} s2;

Applying __align to an array:

In the following example, only arrayA is aligned on a 64-byte boundary, and elements within that array are aligned according to the alignment of AnyType. Padding is applied before the beginning of the array and does not affect the size of the array member itself.

AnyType __align(64) arrayA[10];

Applying __align where the size of the variable alignment differs from the size of the type alignment:

__align(64) struct S {int i;};

// Error: alignment of variable is smaller than alignment of type.
struct S __align(32) s1;

// s2 is aligned on 128-byte boundary
struct S __align(128) s2;

// Error
struct S __align(16) s3[10];

// Error
int __align(1) s4;

// Error
__align(1) struct S {int i;};