-
size of struct
struct ip {
unsigned char ip_length:4;
unsigned char ip_version:4;
unsigned char ip_tos;
unsigned short ip_total_length;
unsigned short ip_id;
unsigned short ip_flags;
unsigned char ip_ttl;
unsigned char ip_protocol;
unsigned short ip_cksum;
unsigned int ip_source;
unsigned int ip_dest;
};
please can anyone please tell why the size of the struct is 24 bytes though if i take ip_length:4 and ip_version:4 out from the structure still the size remains the same .
i tried playing with the structure a lot and am not sure why the size of 2 integers and one char is 12 bytes
thanx
rohit
-
structures are normally padded so that they fit on paragraph boundaries in memory.
-
Fields can only be declared as ints... although if this is something that's giving your compiler pains, I assume it would give a warning.
-
thanx but i need to define the ip structure field exactly as of 20 bytes but if i use
unsigned int version:4;
unsigned int length:4;
then the structure size goes to 24 bytes but if i use the char then it's ok ...
the structure is quite like the same as in <netinet/ip.h> in linux .the same is 20bytes in size in linux too for a similar structure .
i dunno why the compiler is making such a difference when both the OS are 32 bit .
-
>i need to define the ip structure field exactly as of 20 bytes
Then you have to lookup your compiler specifications in your helpfile. You need to turn of structure padding.