当前位置:网站首页>C language__ attribute__ (packed) attribute (learn)

C language__ attribute__ (packed) attribute (learn)

2022-06-21 18:32:00 I'm not xiaohaiwa~~~~

It is the first time to see a structure written like this , I went to Baidu

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)) Is used to tell the compiler to cancel the optimized alignment of structures during the compilation process , Align according to the actual number of bytes occupied , yes GCC Special grammar . This function has nothing to do with the operating system , It's about the compiler ,gcc The compiler is not compact , I am here windows Next , use vc The compiler is not compact , use tc The compiler is compact . for example :html

stay TC Next :struct my{ char ch; int a;} sizeof(int)=2;sizeof(my)=3;( Compact mode )windows

stay GCC Next :struct my{ char ch; int a;} sizeof(int)=4;sizeof(my)=8;( Non compact mode ) Array

stay GCC Next :struct my{ char ch; int a;}attrubte ((packed)) sizeof(int)=4;sizeof(my)=5 The Internet

原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211657356688.html