5.5 #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.

Note:

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

Syntax

#pragma pack([n])

#pragma pack(push[,n])

#pragma pack(pop)

Where:

n
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.
push[,n]
Pushes the current alignment setting on an internal stack and then optionally sets the new alignment.
pop
Restores the alignment setting to the one saved at the top of the internal stack, then removes that stack entry.

Note:

#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).

Default

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

Example

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
To view this graphic, your browser must support the SVG format. Either install a browser with native support, or install an appropriate plugin such as Adobe SVG Viewer.


The layout of SP is:

Figure 5-2 Packed structure SP
To view this graphic, your browser must support the SVG format. Either install a browser with native support, or install an appropriate plugin such as Adobe SVG Viewer.


Note:

In this layout, x denotes one byte of padding.

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

Non-ConfidentialPDF file icon PDF version100067_0609_00_en
Copyright © 2014–2017 Arm Limited (or its affiliates). All rights reserved.