IM有一個結構來表示一個IP報頭SIZEOF不會顯示真實尺寸,試圖實現IP
struct IPHDR // the ip header
{
unsigned char VER : 4;
unsigned char IHL : 4;
unsigned char DSCP : 6;
unsigned char ECN : 2;
unsigned short len;
unsigned short id;
u_char Reserved:1; //Zeroes
u_char DF:1; //Dont Fragment
u_char MF:1; //More Fragment
unsigned short FragOffset : 13;
u_char ttl;
u_char protocol;
u_short checksum;
struct in_addr src;
struct in_addr dst;
} ;
的sizeof(IPHDR)返回24時,它應該返回20 有什麼不好?
這是因爲你的結構被填充。如果支持,您可以使用'#pragma pack'指令來解決這個問題。在這裏檢查http://stackoverflow.com/questions/8322439/what-is-the-smallest-size-i-can-make-this-structure-on-a-64bit-machine/8322469#8322469 – Cyclonecode