__align Specifier

Syntax

declarator __align (integer_constant) identifier;
struct_or_union_specifier __align (integer_constant) [identifier {struct_declaration_list}];

where:

integer_constant Specifies a byte-alignment boundary. integer_constant must be an integer greater than 0 and equal to a power of 2.

Purpose
Use the __align specifier to explicitly specify alignment and padding when declaring or defining data items.

Notes
The __align specifier can only be used with declarations of first-level variables and aggregate definitions. It ignores parameters and automatics.

The __align specifier cannot be used on individual elements within an aggregate definition, but it can be used on an aggregate definition nested within another aggregate definition.

The __align specifier cannot be used in the following situations:

Examples

Applying __align to first-level variables:

int __align(1024) varA;           /* varA is aligned on a 1024-byte boundary and

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

                                     padded with 508 bytes                        */
int __align(128) functionB( );    /* An error                                     */
typedef int __align(128) T;       /* An error                                     */
__align enum C {a, b, c};         /* An error                                     */

 

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

__align(1024) struct structA {int i; int j;};  /* struct structA is aligned on a 

                                                  1024-byte boundary with size 

                                                  including padding of 1024 bytes */
__align(1024) union unionA {int i; int j;};    /* union unionA is aligned on a 

                                                  1024-byte boundary with size 

                                                  including padding of 1024 bytes */

 

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

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

struct S sarray[10];                  /* sarray is aligned on 128-byte boundary 

                                         with sizeof(sarray) == 1280              */

struct S __align(64) svar;            /* error - alignment of variable is smaller 

                                         than alignment of type                   */

struct S2 {struct S s1; int a;} s2;   /* s2 is aligned on 128-byte boundary with 

                                         sizeof(s2) == 256 bytes                  */

 

Applying __align to an array:

AnyType __align(64) arrayA[10];   /* 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 after the back of the 

                                     array and does not affect the size of the 

                                     array member itself.                                */

 

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

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

struct S __align(32) s1;          /* error, alignment of variable is smaller 

                                     than alignment of type                       */

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

struct S __align(16) s3[10];      /* error                                        */
int __align(1) s4;                /* error                                        */
__align(1) struct S {int i;};     /* error                                        */

 



align Compiler Option
RISC System/6000 Alignment Rules
Macintosh and Twobyte Alignment Rules
Alignment Rules for Nested Aggregates