4.36 Unaligned fields in structures

You can use the __packed qualifier to create unaligned fields in structures. This saves space because the compiler does not need to pad fields to their natural size boundary.

For efficiency, fields in a structure are positioned on their natural size boundary. This means that the compiler often inserts padding between fields to ensure that they are naturally aligned.
When space is at a premium, you can use the __packed qualifier to create structures without padding between fields. Structures can be packed in the following ways:
  • The entire struct can be declared as __packed. For example:
    __packed struct mystruct
    {
        char c;
        short s;
    } // not recommended
    
    Each field of the structure inherits the __packed qualifier.
    Declaring an entire struct as __packed typically incurs a penalty both in code size and performance.
  • Individual non-aligned fields within the struct can be declared as __packed. For example:
    struct mystruct
    {
        char c;
        __packed short s; // recommended
    }
    
    This is the recommended approach to packing structures.

Note

The same principles apply to unions. You can declare either an entire union as __packed, or use the __packed attribute to identify components of the union that are unaligned in memory.
Non-ConfidentialPDF file icon PDF versionARM DUI0375F
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.