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

Tony Josi tonyjosinew@gmail.com
Thu Aug 17 03:43:32 GMT 2023


When adding the -Wpacked option to arm-none-eabi-gcc compiler 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 a warning. Though the warning is listed as associated
with: -Wattributes. It doesn't show up if -Wpacked is not given as a
compile option.

Regards,
*Tony*


More information about the Gcc-help mailing list