__packed__
主要用于C/C++ 结构体中内存是否对齐
例如
struct st_packed {
int s0;
uint8_t s1;
int s2;
} __attribute__ ((__packed__));
struct st_align {
int s0;
uint8_t s1;
int s2;
};
sizeof(st_packed) 9
sizeof(st_align) 12
比较适合在申请一块内存时候作为头部结构体
例如
struct st_proto {
uint8_t version;
uint8_t type;
int size;
uint8_t data[];
} __attribute__ ((__packed__));
struct st_data {
int flag;
int res_code;
} __attribute__ ((__packed__));
struct st_com {
st_proto proto;
st_data data;
} __attribute__ ((__packed__));
这样的结构体结构紧密,适合做自定义二进制协议,可以直接用于网络发送(需要注意把int等类型等转为网络字节序)
原文链接: https://www.cnblogs.com/stdpain/p/12435365.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/194051
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!