You copied the Doc URL to your clipboard.


The __packed qualifier sets the alignment of any valid type to 1. This means that:

  • there is no padding inserted to align the packed object

  • objects of packed type are read or written using unaligned accesses.

The __packed qualifier applies to all members of a structure or union when it is declared using __packed. There is no padding between members, or at the end of the structure. All substructures of a packed structure must be declared using __packed. Integral subfields of an unpacked structure can be packed individually.


The __packed qualifier is useful to map a structure to an external data structure, or for accessing unaligned data, but it is generally not useful to save data size because of the relatively high cost of unaligned access. Only packing fields in a structure that requires packing can reduce the number of unaligned accesses.


On ARM processors that do not support unaligned access in hardware, for example, pre-ARMv6, access to unaligned data can be costly in terms of code size and execution speed. Data accesses through packed structures must be minimized to avoid increase in code size and performance loss.


The following restrictions apply to the use of __packed:

  • The __packed qualifier cannot be used on structures that were previously declared without __packed.

  • Unlike other type qualifiers you cannot have both a __packed and non-__packed version of the same structure type.

  • The __packed qualifier does not affect local variables of integral type.

  • A packed structure or union is not assignment-compatible with the corresponding unpacked structure. Because the structures have a different memory layout, the only way to assign a packed structure to an unpacked structure is by a field-by-field copy.

  • The effect of casting away __packed is undefined, except on char types. The effect of casting a nonpacked structure to a packed structure, or a packed structure to a nonpacked structure, is undefined. A pointer to an integral type that is not packed can be legally cast, explicitly or implicitly, to a pointer to a packed integral type.

  • There are no packed array types. A packed array is an array of objects of packed type. There is no padding in the array.


Taking the address of a field in a __packed structure or a __packed-qualified field yields a __packed-qualified pointer. The compiler will produce a type error if you attempt to implicitly cast this pointer to a non-__packed pointer. This contrasts with its behavior for address-taken fields of a #pragma packed structure.


Example 8 shows that a pointer can point to a packed type.

typedef __packed int* PpI;          /* pointer to a __packed int */
__packed int *p;                    /* pointer to a __packed int */
PpI p2;                             /* 'p2' has the same type as 'p' */
                                    /* __packed is a qualifier  */
                                    /* like 'const' or 'volatile' */
typedef int *PI;                    /* pointer to int */
__packed PI p3;                     /* a __packed pointer to a normal int */
                                    /*  -- not the same type as 'p' and 'p2' */
int *__packed p4;                   /* 'p4' has the same type as 'p3' */

Example 9 shows that when a packed object is accessed using a pointer, the compiler generates code that works and that is independent of the pointer alignment.

typedef __packed struct
    char x;                   // all fields inherit the __packed qualifier
    int y;
} X;                          // 5 byte structure, natural alignment = 1
int f(X *p)
    return p->y;              // does an unaligned read
typedef struct
    short x;
    char y;
    __packed int z;           // only pack this field
    char a;
} Y;                          // 8 byte structure, natural alignment = 2
int g(Y *p)
    return p->z + p->x;       // only unaligned read for z