You copied the Doc URL to your clipboard.

#pragma pack(n)

This pragma aligns members of a structure to the minimum of n and their natural alignment. Packed objects are read and written using unaligned accesses.

Note

This pragma is a GNU compiler extension that the ARM compiler supports.

Syntax

#pragma pack(n)

Where:

n

is the alignment in bytes, valid alignment values being 1, 2, 4 and 8.

Default

The default is #pragma pack(8).

Example

This example demonstrates how pack(2) aligns integer variable b to a 2-byte boundary.

typedef struct
{ 
    char a;
    int b;
} S;
#pragma pack(2)
typedef struct
{ 
    char a;
    int b;
} SP;
S var = { 0x11, 0x44444444 };
SP pvar = { 0x11, 0x44444444 };

The layout of S is:

Figure 4-1 Nonpacked structure S


The layout of SP is:

Figure 4-2 Packed structure SP


Note

In this layout, x denotes one byte of padding.

SP is a 6-byte structure. There is no padding after b.

Was this page helpful? Yes No