Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

aligned pragma

&pagelevel(4)&pagelevel

The aligned pragma can be used to align data elements within classes, structures and unions on a larger number of bytes than the default minimum alignment set for the compiler.
This pragma can be used in all language modes of the compiler.


#pragma aligned n


n is a number of bytes, which is specified in steps to the power of 2 up to a maximum of 8, and optionally entered in decimal, octal or hexadecimal notation. The permitted values (in decimal notation) are 1, 2, 4, 8 and16 (still permitted by the syntax).

Notes
For the sake of simplicity, the following notes only refer to structures, but are also analogously applicable to classes and unions.

  • Pragmas that specify fewer bytes than the minimum alignment intended for thecorresponding data type (see table below) are not permitted and are ignored.

    Data type

    Minimum alignment
    (number of bytes)

    char

    short

    int

    4

    long

    4

    long long

    8

    float

    4

    pointer

    4

    double

    8

    long double

    8

  • The data element to be aligned may also be a bitfield, in which case the bitfield is created in a new base field with the required alignment.

  • In the case of static elements, the pragma is ignored.

  • The pragma must be placed in the structure definition immediately before the data element to be aligned. Otherwise, it will be ignored.

  • If multiple pragmas precede a data element, the one with the largest alignment specification is considered.

  • If a pragma precedes the declaration of several structure elements, it is applied to only the first declared element.

  • The alignment of a structure is based on the maximum alignment of its elements.

  • If a structure appears as an element in another structure, the pragma preceding it applies to the alignment of the entire structure, and not the alignment of its elements.

  • A pragma that precedes a structure element that represents an array applies to the alignment of the entire array (i.e. the first array element), and not the remaining array elements.

  • Aligned pragmas and pack pragmas (see "pack pragma") can be active at the same time for a specified structure element. In this case, the aligned pragmas take precedence.

Example

... 
class bsp1 
  {
    int a;                  // Aligned on 4 bytes. 
#pragma aligned 8 
    int b,c;                // b is aligned on 8 bytes. 
                            // c is aligned on 4 bytes! 
                            // The maximum alignment of an element
                            // of class bsp1 is thus equal to 8 bytes.
                            // Class bsp1 is therefore aligned on
                            // 8 bytes.
    int d;                  // Aligned on 4 bytes. 
  };
class bsp2 
  {
public: 
double dens;                // Aligned on 8 bytes. 
#pragma aligned 4           // Is ignored. Since the maximum alignment
                            // of an element of the structure stru1 is 
                            // 8 bytes, stru1 is also aligned on 8 bytes.
                            // A corresponding warning is issued.
    struct 
      {
        int istru11;        // Aligned on 4 bytes. 
        double dstru12;     // Aligned on 8 bytes. 
      } stru1;              // Aligned on 8 bytes (see above). 
    struct 
      { 
        short s1;           // Aligned on 2 bytes. 
        short s2;           // Aligned on 2 bytes. 
      } stru2;              // Aligned on 2 bytes, since the maximum
                            // alignment of an element equals 2 bytes.
#pragma aligned 4 
    char c;                 // Aligned on 4 bytes.
    char c1;                // Aligned on 1 byte.
#pragma aligned 8 
    short ar1[16];          // The array is aligned on 8 bytes, 
                            // but not the individual array elements.
    ... 
  } 
...