3.23 __attribute__((packed)) type attribute

The packed type attribute specifies that a type must have the smallest possible alignment. This attribute only applies to struct and union types.

Note

You must access a packed member of a struct or union directly from a variable of the containing type. Taking the address of such a member produces a normal pointer which might be unaligned. The compiler assumes that the pointer is aligned. Dereferencing such a pointer can be unsafe even when unaligned accesses are supported by the target, because certain instructions always require word-aligned addresses.

Example 3-1 Examples

struct __attribute__((packed)) foobar 
{
  char x;
  short y;
}; 

short get_y(struct foobar *s) 
{
    // Correct usage: the compiler will not use unaligned accesses
    // unless they are allowed.
    return s->y;
} 

short get2_y(struct foobar *s) 
{
    short *p = &s->y; // Incorrect usage: 'p' might be an unaligned pointer.
    return *p;  // This might cause an unaligned access.
}
Related reference
1.42 -munaligned-access, -mno-unaligned-access
Non-ConfidentialPDF file icon PDF versionARM DUI0774E
Copyright © 2014-2016 ARM. All rights reserved.