第一次看见结构体这样写,便去百度了一下
typedef struct param
{
unsigned char KEY_TYPE;
unsigned char KEY_LEN;
unsigned char KEY_INDEX;
unsigned char Mode;
unsigned char key_data[24];
} __attribute__ ((packed))param;
1. attribute ((packed)) 的做用就是告诉编译器取消结构在编译过程当中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操做系统不要紧,跟编译器有关,gcc编译器不是紧凑模式的,我在windows下,用vc的编译器也不是紧凑的,用tc的编译器就是紧凑的。例如:html
在TC下:struct my{ char ch; int a;} sizeof(int)=2;sizeof(my)=3;(紧凑模式)windows
在GCC下:struct my{ char ch; int a;} sizeof(int)=4;sizeof(my)=8;(非紧凑模式)数组
在GCC下:struct my{ char ch; int a;}attrubte ((packed)) sizeof(int)=4;sizeof(my)=5网络
最后
以上就是现实鱼最近收集整理的关于C语言__attribute__(packed)属性(学习一下)的全部内容,更多相关C语言__attribute__(packed)属性(学习一下)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复