大佬教程收集整理的这篇文章主要介绍了结构成员对齐 – 使用16位和32位编译器的不同sizeof,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
typedef struct { unsigned char new_weld_status[2]; UINT32 new_weld_count; UINT16 new_weld_fail_count; } NEW_PULSE_DATA;
数组new_weld_status [2]在16位编译器上占用2个字节,在32位编译器上占用4个字节.我正在考虑用gcc编译时用union替换所有new_weld_status [2].但是有一个我可以用于gcc的开关,使chars适合/对齐2个字节?
谢谢
typedef struct { unsigned char new_weld_status[2]; //a //char padding_1[2]; //hidden padding UINT32 new_weld_count; //a UINT16 new_weld_fail_count; //a } NEW_PULSE_DATA;
以下对结构的重新定义完全避免了这个问题.
typedef struct { UINT32 new_weld_count; //a UINT16 new_weld_fail_count; //a unsigned char new_weld_status[2]; //a } NEW_PULSE_DATA; NEW_PULSE_DATA ex_PULSE_DATA;
然而,上述方法通常不是通过网络/通过消息传输来传输结构(ured)数据的方法.一种更常见且更好的方法是使用序列化/反序列化层(也称为编组)将结构放置在“线上”格式中.您当前的方法是将内存存储和寻址与通信格式混为一谈.
//you need to decide on the size of wire format data,//Both ends of the protocol must agree on these sizes,#define new_weld_count_SZ sizeof(ex_PULSE_DATA.new_weld_count) #define new_weld_fail_count_SZ sizeof(ex_PULSE_DATA.new_weld_fail_count) #define new_weld_status_SZ sizeof(ex_PULSE_DATA.new_weld_status) //Then you define a network/message format typedef struct { byte new_weld_count[new_weld_count_SZ]; byte new_weld_fail_count[new_weld_count_SZ]; byte new_weld_status[new_weld_count_SZ]; } messaGE_FORMAT_PULSE_DATA;
然后你将实现序列化&反序列化功能在传输的两端.下面的例子很简单,但传达了你需要的要点.
byte* PULSE_DATA_serialize( messaGE_FORMAT_PULSE_DATA* msg,NEW_PULSE_DATA* data ) { memcpy(&(msg->new_weld_count),data->new_weld_count,new_weld_count_Sz); memcpy(&(msg->new_weld_fail_count),data->new_weld_fail_count,new_weld_fail_count_Sz); memcpy(&(msg->new_weld_status),data->new_weld_status,new_weld_status_Sz); return msg; } NEW_PULSE_DATA* PULSE_DATA_deserialize( NEW_PULSE_DATA* data,messaGE_FORMAT_PULSE_DATA* msg ) { memcpy(data->new_weld_count,&(msg->new_weld_count),new_weld_count_Sz); memcpy(data->new_weld_fail_count,&(msg->new_weld_fail_count),new_weld_fail_count_Sz); memcpy(data->new_weld_status,&(msg->new_weld_status),new_weld_status_Sz); return msg; }
请注意,我省略了必须的网络字节顺序转换,因为我假设您已经解决了两个cpu域之间的字节顺序问题.如果你没有考虑字节顺序(big-endian和little-endian),那么你也需要解决这个问题.
发送邮件时,发件人执行以下操作,
//you need this declared & assigned somewhere NEW_PULSE_DATA data; //You need space for your message messaGE_FORMAT_PULSE_DATA msg; result = send(PULSE_DATA_deserialize( &data,&msg ));
收到邮件时,收件人会执行以下操作,
//recipient needs this declared somewhere NEW_PULSE_DATA data; //Need buffer to store received data messaGE_FORMAT_PULSE_DATA msg; result = receive(&msg,sizeof(msg)); //appropriate receipt checking here... PULSE_DATA_deserialize( &data,&msg );
以上是大佬教程为你收集整理的结构成员对齐 – 使用16位和32位编译器的不同sizeof全部内容,希望文章能够帮你解决结构成员对齐 – 使用16位和32位编译器的不同sizeof所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。