You copied the Doc URL to your clipboard.

Arm Compiler armclang Reference Guide : #pragma pack(...)

#pragma pack(...)

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. You can optionally push and restore alignment settings to an internal stack.


This pragma is a GNU compiler extension that the Arm® compiler supports.


#pragma pack([n])

#pragma pack(push[,n])

#pragma pack(pop)


Is the alignment in bytes, valid alignment values are 1, 2, 4, and 8. If omitted, sets the alignment to the one that was in effect when compilation started.
Pushes the current alignment setting on an internal stack and then optionally sets the new alignment.
Restores the alignment setting to the one saved at the top of the internal stack, then removes that stack entry.


#pragma pack([n]) does not influence this internal stack. Therefore, it is possible to have #pragma pack(push) followed by multiple #pragma pack([n]) instances, then finalized by a single #pragma pack(pop).


The default is the alignment that was in effect when compilation started.


This example shows 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 5-1 Nonpacked structure S

The layout of SP is:

Figure 5-2 Packed structure SP


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