I have a structure specified as following
- Member 1, 16 bits
- Member 2, 32 bits
- Member 3, 32 bits
which I shall be reading from a file. I want to read straight from the file into the struct.
The problem is that the C compiler will align the variables m1, m2 and m3 to word boundaries which are at 32 bits since I am working on an ARM Cortex M3 for the following struct declaration:
typedef struct { uint16_t m1; uint32_t m2; uint32_t m3; }something; Reading directly from file will put wrong values in m2 and m3, and reads 2 extra bytes too.
I have hacked around and am currently using the following which works just fine:
typedef struct { uint16_t m1; struct { uint16_t lo; uint16_t hi; }m2; struct { uint16_t lo; uint16_t hi; }m3; }something; However, this looks like a really dirty hack. I cannot help wishing for a cleaner way to force the compiler to put halves of m2 and m3 in different words, however sub-optimal it may be.
I am using arm-none-eabi-gcc. I know about bit packing, but am unable to work around this optimisation.
Edit: Turns out I didn't know enough about bit-packing :D
arm-none-eabi-gccis working very well withpackedattribute (unless compiled with some weird settings, I guess..). Well, maybe some ancient versions aren't...