I have a representation of an IP header in C with bit-precision fields:
typedef struct __attribute__((packed)) {
unsigned char __reserved : 1;
unsigned char dont_fragment : 1;
unsigned char more_fragment : 1;
unsigned short fragment_offset : 13; // if fragmented, in 8 byte units from the start of the datagram
} ipv4_fragmenting;
I use 16 bits that can be stored on 2 bytes. So why is the size of the structure (sizeof(ipv4_fragmenting)
) is 4 instead of 2?
My compiler: GCC 4.8.1
Edit:
If bitfields are so platform-specific and packed attribute is unreliable what would be the correct solution to represent elements of previously defined protocols like IPv4?