In GCC C, how can I get an array of packed structs, where each entry in the array is aligned?
(FWIW, this is on a PIC32, using the MIPS4000 architecture).
I have this (simplified):
typedef struct __attribute__((packed)) { uint8_t frameLength; unsigned frameType :3; uint8_t payloadBytes; uint8_t payload[RADIO_RX_PAYLOAD_WORST]; } RADIO_PACKET; RADIO_PACKETs are packed internally.
Then I have RADIO_PACKET_QUEUE, which is a queue of RADIO_PACKETs:
typedef struct { short read; // next buffer to read short write; // next buffer to write short count; // number of packets stored in q short buffers; // number of buffers allocated in q RADIO_PACKET q[RADIO_RX_PACKET_BUFFERS]; } RADIO_PACKET_QUEUE; I want each RADIO_PACKET in the array q[] to start at an aligned address (modulo 4 address).
But right now GCC doesn't align them, so I get address exceptions when trying to read q[n] as a word. For example, this gives an exception:
RADIO_PACKET_QUEUE rpq; int foo = *(int*) &(rpq.q[1]); Perhaps this is because of the way I declared RADIO_PACKET as packed.
I want each RADIO_PACKET to remain packed internally, but want GCC to add padding as needed after each array element so each RADIO_PACKET starts at an aligned address.
How can I do this?
RADIO_PACKETas packed when misalignment is a problem? You do realize that packed structures are not very useful on platforms where misaligned memory access is prohibited as the members of packed structures are not aligned?RADIO_PACKETnot packed. Do you know what a packed declaration means? (hint: it tells the compiler to leave out any padding).