头文件内定义了:
typedef struct {
unsigned char headtag;
unsigned int ipacket;
unsigned short packetLen;
unsigned short checkcode;
} tagUDPHead;
cpp 文件里面:
159 cout<<"tagUDPHead:"<<sizeof(tagUDPHead)<<endl;
(gdb) n
tagUDPHead:12
(gdb) p sizeof(unsigned char)
$1 = 1
(gdb) p sizeof(unsigned int)
$2 = 4
(gdb) p sizeof(unsigned short)
$3 = 2
怎么加起来应该是9,为什么打印结果是12呢?
我继续修改cpp,下面是GDB的显示:
168 cout<<"tagUDPHead1:"<<sizeof(tagUDPHead1)<<endl;
(gdb) l 158
153 m_SocketID = -1;
154 }
155 }
156
157 struct tagUDPHead1 {
158 unsigned char headtag;
159 unsigned int ipacket;
160 unsigned short packetLen;
161 unsigned short checkcode;
162 } ;
(gdb) n
tagUDPHead1:12
169 cout<<"headtag:"<<sizeof(a.headtag)<<endl;
(gdb)
headtag:1
170 cout<<"ipacket:"<<sizeof(a.ipacket)<<endl;
(gdb)
ipacket:4
171 cout<<"packetLen:"<<sizeof(a.packetLen)<<endl;
(gdb)
packetLen:2
172 cout<<"checkcode:"<<sizeof(a.checkcode)<<endl;
(gdb)
checkcode:2
只要在定义前加一个
#pragma pack(1)
typedef struct
{
unsigned char headtag; //'#'
unsigned long ipacket; //当前包号,用以确认包的连续性及丢包情况,每天起始包从0开始
unsigned short packetLen; //当前UDP包的长度
unsigned short checkcode; //对除包头外的内容的CRC16校验
} tagUDPHead;
#pragma pack()
就OK了.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。