徐如林的博客

2019年11月15日星期五晚上8点理发

关于结构体在传递至需要字节对齐的编码方式

在传递消息是往往结构体成员之间没有“空隙”,如果不做处理很容易发生第一组配置是好的 ,后面的就错位了

在定义结构体时添加__attribute__ ((packed))

可以使字节对齐

 

如   typedef struct qosset_t{

    uint32_t   target ;

    uint8_t protocol_type;

    uint32_t  bytes;

} qosset_t;

 

占字节  为    4+4+4

 

  typedef struct qosset_t{

    uint32_t   target ;

    uint8_t protocol_type;

    uint32_t  bytes;

} __attribute__ ((packed))qosset_t;

 占字节  为    4+1+4

posted on 2020-01-10 21:00  骑象人的烦恼  阅读(194)  评论(0)    收藏  举报

导航