user2752256
user2752256

Reputation: 13

gcc __packed__ does not work as expected

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?

Upvotes: 1

Views: 819

Answers (1)

Konstantin Vladimirov
Konstantin Vladimirov

Reputation: 7009

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.

Upvotes: 1

Related Questions