arm-none-eabi-gcc: packed attribute causes inefficient alignment

111 Views Asked by At

When adding the -Wpacked option to arm-none-eabi-gcc combiler it issues the following warning:

warning: packed attribute causes inefficient alignment for 'struct field name'

For example,

struct Struct_test 
{
    uint32_t ch1;
    uint16_t ch2;
    uint32_t ch3;
}
__attribute__( ( packed ) );

warns about the fields ch1 and ch2:

test.c:12:14: warning: packed attribute causes inefficient alignment for 'ch1' [-Wattributes]
   12 |     uint32_t ch1;
      |              ^~~
test.c:13:14: warning: packed attribute causes inefficient alignment for 'ch2' [-Wattributes]
   13 |     uint16_t ch2;
      |              ^~~

As per the manual -Wpacked means: Warn when the packed attribute has no effect on struct layout. But in this case the packed attribute does have an impact on the struct layout as it removes the padding between ch2 and ch3, but still it causes warning.

Edit: Though the warning is listed as associated with: -Wattributes. It doesn't show up if -Wpacked is not given as compile option.

1

There are 1 best solutions below

4
Simon Goater On

I don't have an ARM CPU to test this on but I think I see the problem. I think if you change your struct to

struct Struct_test 
{
    uint32_t ch1;
    uint32_t ch3;
    uint16_t ch2;
}
__attribute__((aligned(4)))
__attribute__( ( packed ) );

then the issue will go away, unless you make an array of these structs. An array of these packed structs will have alignment issues which may cause undefined behaviour if you attempt to dereference the elements. You have to take care with how you access the data in packed structs with alignment issues. One safe way is to memcpy to a local variable.

In general, it's a good idea to arrange struct elements in a way that conserves space. A simple way to do this is to put the elements in increasing or decreasing order of size.