I was recently introduced to bit fields. I have a following union.
typedef struct
{
uint16_t var1:16;
uint32_t var2:28;
uint8_t var3:8;
uint8_t var4:8;
uint8_t var5:8;
uint8_t var6:8;
bool var7:1;
bool var8:1;
bool var9:1;
bool var10:1;
bool var11:1;
uint8_t var12:1;
uint8_t var13:7;
uint8_t var14:7;
uint32_t var15:18;
uint16_t var16:10;
uint8_t var17:4;
} packet_bit_map;
typedef union
{
packet_bit_map packetsArrived ;
uint8_t packetRaw[16];
} packetDecode;
the idea is to copy the data stream coming from external device which is 16 bytes individual byte values and then use the bit fielded structure to access particular information. But I was not able to do it after debugging I found out that the size of union packetDecode ended up being 20 and not 16 as expected because packetsArrived is holding 20bytes . Why is this happening? and how to avoid this padding?
EDIT: I know one solution that is to use attribute padding but sadly i cannot use inline function in my project.