You copied the Doc URL to your clipboard.

__attribute__((packed)) type attribute

The packed type attribute specifies that a type must have the smallest possible alignment.

Note

This type attribute is a GNU compiler extension that the ARM compiler supports.

In non-GNU mode, this attribute is equivalent to __packed, and has stronger constraints than when used in GNU-mode.

GNU mode

To enable GNU-mode, use the --gnu option.

In GNU mode, __attribute__((packed)) has the effect of #pragma packed. Taking the address of a field covered by __attribute__((packed)) does not produce a __packed qualified pointer.

In non-GNU-mode, __attribute__((packed)) has the effect of __packed. Taking the address of a field covered by __attribute__((packed)) produces a __packed qualified pointer.

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

short get_y0(struct foobar *s){    
    return *s->y;  // Unaligned-capable load
}

short *get_y(struct foobar *s){
    return s->y;   // Compile error unless --gnu is used.
}

Errors

Taking the address of a field with the packed attribute or in a structure with the packed attribute yields a __packed-qualified pointer. The compiler produces 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.

The compiler generates a warning message if you use this attribute in a typedef.

Was this page helpful? Yes No