I want to use this struct in visual c++:
struct iphdr
{
unsigned int ihl:4;
unsigned int version:4;
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
struct in_addr ip_src;
struct in_addr ip_dst;
};
but when debugging I found sizeof(struct iphdr) = 24, which expected to be 20
And it is strange that when I replace ihl and version with a char, sizeof(struct iphdr) = 20
The bitfields
ihlandversionwill be placed in anunsigned intfield (despite the fact that they would fit into a character). Hence, the fieldtoswill be placed at offset 4, andtot_lengthat offset 6, due to padding.