当前位置:网站首页>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
边栏推荐
- Asynchronous programming promise
- MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
- Operation document tree
- Esxi community network card driver
- Group management and permission management
- 登录进oracle10g的oem,想管理监听程序却总是弹出帐号密码输入页面
- Standard C language learning summary 6
- Canvas drawing 1
- shell---函数
- JSON notes
猜你喜欢

Joern's code uses -devign

MOOC Weng Kai C language week 7: array operation: 1. array operation 2. Search 3. preliminary sorting

MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation

Group management and permission management

Shell--第一天作业

Canvas drawing 1

SySeVR环境配置:joern-0.3.1、Neo4j-2.1.5、py2neo2.0

远程访问云服务器上Neo4j等服务的本地网址

Icc2 analysis timing artifact analyze_ design_ violations

Easypoi export interlaced style settings
随机推荐
MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays
Joern's code uses -devign
DOM operation cases
Result number of filled briquettes
爬虫学习总结
Redis哨兵模式及集群
关于正则的教程
Blue Bridge Cup square filling number
Install Nessus under win
Operation document tree
kali下安装nessus
一个定时任务提醒工具
MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
Read the IP and device information of the switch node in the XML file, Ping the device, and the exception is displayed in the list
Implementation method of Bert
DHCP服务
DNS域名解析
Differences and relationships among NPM, Yran and NPX
Detailed explanation of active scanning technology nmap
Shell--- function