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

Show/hideSyntax

#pragma pack(n)

Where:

n

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

Show/hideDefault

The default is #pragma pack(8).

Show/hideExample

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 as shown in Figure 1, while the layout of SP is as shown in Figure 2. In Figure 2, x denotes one byte of padding.

Figure 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.


Figure 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

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

Show/hideSee also

Copyright © 2007-2008, 2011 ARM. All rights reserved.ARM DUI 0376C
Non-ConfidentialID061811