This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

create a packed struct

Long story short, I need to create a packed struct.

I knew the pragma way, to change aligment, didn't work. I looked up and found the __packed (I'm using Keil µVision) here: www.keil.com/.../armcc_chr1359124229695.htm

So in the end, here's my theoritically bulletproof code:

#pragma pack(push)
#pragma pack(1)

__packed struct PackedStructure
{
  __packed uint16_t packedValue;
};

#pragma pack(pop)

And now what does sizeof(struct PackedStructure) return ? 4...

Note I've tried pretty much any combination of pragma pack and __packed on structure/member, with always the same result.

And this:

typedef __packed __attribute__((packed)) __attribute__((aligned(1))) struct
{
  __packed uint16_t packedValue;
  __packed uint16_t packedValue2;
} Event;

Gives me a size of 8.

Anyone can tell me how to get a packed structure? Thanks!

Related