gcc __packed__ does not work as expected

827 views Asked by At

I frequently use code like this:

struct teststruct
{
    uint8_t i1;
    uint16_t i2;
    uint32_t i4;
} __attribute__((__packed__));
struct teststruct *protocol = (struct teststruct *)buffer;
uint16_t var = protocol->i2;

In order to access protocol data via structs.

The code works for AVR gcc 4.6, 4.7 and Win32 4.6, 4.7 and Linux 4.6 However now from (MingW) gcc 4.8 it does not work as expected. sizeof(struct teststruct) will return 8.

I did not found any hints why it does not work anymore. Or is there an other way to access a protocol buffer a structural way?

1

There are 1 answers

0
Konstantin Vladimirov On BEST ANSWER

Seems that compilation with -mno-ms-bitfields must help (see extended discussion on the GCC bugzilla). I have no mingw at hands, but I created simple reproduction:

#include <stdint.h>
#include <stdio.h>

struct teststruct
{
    uint8_t i1;
    uint16_t i2;
    uint32_t i4;
} __attribute__((__packed__));

int main(void)
{
  fprintf(stderr, "size = %d\n", sizeof(struct teststruct));
  return 0;
}

And compiled it on linux with -mms-bitfields, so it returns 8. Default is 7. I suppose, -mms-bitfields is default for windows targets.