当前位置:网站首页>Review of C language (byte alignment)
Review of C language (byte alignment)
2022-07-28 07:12:00 【The most beautiful wish must be the craziest】
Byte alignment
Alignment rules :
The first member is offset from the structure variable by 0 The address of .
Other member variables should be aligned to the address of an integer multiple of the alignment number . Align numbers = Compiler default alignment number And The smaller value of the member size .
The total size of the structure is the maximum number of alignments ( Each member variable has an alignment number ) Integer multiple .
If the structure is nested , The nested structure is aligned to an integer multiple of its own alignment number , Alignment number of nested structure = The maximum number of alignments in a nested structure And The smaller value of the default alignment number of the current structure . The overall size of the total structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
//4 Byte alignment struct S1 { char d; double c; // 4+8 The length is 8 Default 4 Byte alignment , So the alignment number is 4 double i; //4+8+8 }; printf("%d\n", sizeof(struct S1)); //20 struct S2 { char c1; struct S1 s1;//4+20 Align the nested structure to its own alignment number 4 At an integral multiple of int d;//4+20+4 }; printf("%d\n",sizeof(struct S2)); // 28 Because the maximum parity of the largest whole is 4, So there is no need to make up//8 Byte alignment struct S1 { char d; double c;// 8+8 The length is 8 Default 8 Byte alignment , So the alignment number is 8 double i;//8+8+8 }; printf("%d\n", sizeof(struct S1)); //24 struct S2 { char c1; struct S1 s1; //8+24 Align the nested structure to its own alignment number 8 At an integral multiple of int d;//8+24+4 }; printf("%d\n",sizeof(struct S2)); // 40 After alignment is 36, Because the maximum number of alignments in a nested structure is 8, So the maximum number of alignments in the total structure is 8, Finally, we need to 8 Byte completion , So for 40// Default 8 Byte alignment struct S1 { char c; double d; // 8+8 The length is 8 Default 8 Byte alignment , So the alignment number is 8 double i; }; printf("%d\n", sizeof(struct S1)); //24 #pragma pack (4) // It's all in the back 4 Byte alignment , Until the end of this document struct S2 { char c1; struct S1 s1; //4+24 Although the alignment number of the previously nested structure to its own is 8 , But the current structure S2 The default alignment number is 4 , therefore S1 The alignment number of is 4 int d;//4+24+4 }; printf("%d\n",sizeof(struct S2)); // 32 Because the maximum number of alignments is 4, So there is no need to make up// Default 8 Byte alignment struct S1 { char c; char d; char i; }; printf("%d\n", sizeof(struct S1)); //3 //#pragma pack (4) struct S2 { char c1; struct S1 s1; int d; }; printf("%d\n",sizeof(struct S2)); // 8struct S1 { char c; char d; char i; char a; char b; }; printf("%d\n", sizeof(struct S1)); //5 //#pragma pack (4) struct S2 { char c1; struct S1 s1; int d; }; printf("%d\n", sizeof(struct S2)); //12
边栏推荐
- 360 compatibility issues
- Standard C language summary 4
- Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
- Pytorch installation - CPU version
- Construction of Yum warehouse
- Joern的代码使用-devign
- win下安装nessus
- Standard C language learning summary 3
- DNS domain name resolution
- MOOC翁恺 C语言 第三周:判断与循环:1.判断
猜你喜欢

Codesensor: convert the code into AST and then into text vector

根据excel生成create建表SQL语句

Group management and permission management

Blue bridge code error ticket

Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec

Easypoi export interlaced style settings

VLAN的配置

freemarker合并单元格,if、else标签的使用,null、空字符串处理

Esxi community network card driver

MySQL build database Series (I) -- download MySQL
随机推荐
New, let, VaR, const and temporary deadband. The difference between XML and JSON
Neo4j running error occurred during initialization of VM incompatible minimum and maximum heap sizes spec
DOM operation cases
Shell--- circular statement practice
Implementation method of Bert
起点中文网 字体反爬技术 网页可以显示数字字母 网页代码是乱码或空格
easypoi导出隔行样式设置
VNC Timed out waiting for a response from the computer
codesensor:将代码转化为ast后再转化为文本向量
joern运行后.joernindex数据库无内容
主动扫描技术nmap详解
Standard C language learning summary 9
Canvas drawing 2
DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
C language maze
Standard C language learning summary 3
Construction of Yum warehouse
Static and floating routes
DHCP service
VNC Timed out waiting for a response from the computer