__attribute_((packed))
来源:互联网 发布:hbase性能优化方法总结 编辑:程序博客网 时间:2024/05/20 22:36
__attribute__((packed))的作用是让编译器取消结构体在编译过程中的优化对齐,可以保证结构体在不同的处理器中有相同的大小。使用示例:
typedef struct __attribute__((packed)) { uint8_t protocolType[12]; uint8_t packetType; uint8_t serviceType; uint8_t optRetCode; uint8_t reserved; uint32_t dataLen;} PktHeader;
下面用一个例子进行解释:
typedef struct { char aChar; int anInt ;} s;
假如处理器是使用八个字节进行优化对齐。那么aChar将占据第1个字节,紧跟的7个字节将不被使用,然后anInt将从第9个字节开始。
假如处理器是使用四个字节进行优化对齐,那么aChar将占据第1个字节,紧跟的4个字节将不被使用,然后anInt将从第5个字节开始。
如果使用了__attribute__((packed))
typedef struct __attribute__((packed)) { char aChar; int anInt ;} s;那么将强制让anInt紧跟在aChar后边,aChar占据第1个字节,而anInt从第2个字节开始。
参考链接:http://www.linuxquestions.org/questions/programming-9/what's-this-__attribute__-packed-193570/
0 0
- __attribute_((packed))
- __attribute_((packed))参数
- __attribute_((naked))
- GCC编译关键字“__attribute_…
- GCC编译关键字“__attribute_…
- Packed record
- _attribute_((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- _attribute_((packed))
- _attribute_((packed))
- _attribute_((packed))
- _attribute_((packed))
- __attribute__((packed))
- __attribute__((packed))
- __attribute__((packed))
- Packed Record
- poj 1703 Find them, Catch them(关系并查集)
- android Application学习之二
- jquery操作select(取值,设置选中)【转】
- iOS绘图教程
- Linux中listen()系统调用的backlog参数分析
- __attribute_((packed))
- 受限玻尔兹曼机(RBM)学习笔记(六)对比散度算法
- android Fragments (Android官方文档中文版)
- float变量与零值的比较 .
- 设置windows磁盘图标
- C++基础知识
- 解开MSF团队管理的秘密
- android application学习之三
- 用dom4j 将xml to String 或者Document