当前位置:网站首页>Knowledge of C language corners of byte alignment
Knowledge of C language corners of byte alignment
2022-07-30 21:12:00 【Gou Bar Listening Song_0】
目录
Then the alignment of the structure
字节对齐
什么是字节对齐
Starting from the efficiency of memory access,CPUThe underlying layer or the compiler will generally require it,The addresses of all objects are aligned in some way.
一般来说,That is, the address of the object is requiredn的倍数.
对齐方式
n字节对齐
对象的地址是n的倍数(n一般为2的x次幂)
eq:
4字节对齐:The address of all objects must be 4的倍数.
8字节对齐:The address of all objects must be 8的倍数.
16字节对齐:
...
GCC代码中:
The programmer can specify the alignment:
__attribute__((aligned(n))
自然对齐
Compiler's default alignment(32bits与64bitsCompilers will be different).
| 32bits | 64bits | |
| char | 1字节对齐 | 1字节对齐 |
| short | 2字节对齐 | 2字节对齐 |
| int | 4字节对齐 | 4字节对齐 |
| long | 4字节对齐 | 8字节对齐 |
| long long | 4字节对齐 | 8字节对齐 |
| any pointer | 4字节对齐 | 8字节对齐 |
| double | 8字节对齐 | 8字节对齐 |
The address of the object must be a multiple of the object length,什么意思呢?
举个栗子:
sizeof(int) = 4;
int a;
&a :必须为4的倍数
if &a不是4的倍数,说明aNot a natural alignment.
sizeof(short) = 2;
short a;
&a : 必须为2的倍数
if &a不是2的倍数,说明aNot a natural alignment
sizeof(long) = 8;
long b;
&b:b的地址必须是8的倍数
sizeof(char) = 1;
char c;
&c:c的地址必须是1 的倍数
每个变量(Include member variables in structures)will have a default alignment:--》自然对齐
Then the alignment of the structure
(1)Structure variables are aligned according to the alignment of their largest type member variable
(2)The size of a structure must be an integer multiple of its alignment(Usually rounded up)
举个例子:
struct test
{
char a; //a的对齐方式:一字节对齐
int b; //b对齐的方式:四字节对齐
short c; //c对齐的方式:2字节对齐
}; struct test 按照balignment method:4字节对齐
struct test 变量 (整个结构体)大小必须是4的整数倍.
sizeof(struct test); ==> 12
think two more
struct finalPadShort
{
short x;
char n[3];
};
sizeof(struct finalPad); //6struct MixedData
{
char data1;
short data2;
int data3;
char data4;
};
sizeof(struct finalPad); //12最后一个为什么是12呢,我们来看图,Here's how it's stored
| 地址低位 | 地址高位 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| char | short | short | X | int | int | int | int | char | X | X | X |
如图,第一个成员变量是char,占1字节,第二个成员变量是short,占2字节,The two member variables do not add up to more than int的4字节,可以放在一个4字节中,所以一共是3个4字节,
sizeof(struct finalPad) == 12.
边栏推荐
- The structure of knowledge in the corners of the C language
- [Machine Learning] The Beauty of Mathematics Behind Gradient Descent
- Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
- MySQL60 homework
- Generate OOM records in a production environment. Conclusion: Don't be lazy to query useless fields unless you are completely sure.
- 深度学习模型训练前的必做工作:总览模型信息
- IDEA2018.3.5 cancel double-click Shift shortcut
- What is the common factor
- 走进Redis,让你重新认识redis。绝不是表面
- Redis数据更新,是先更新数据库还是先更新缓存?
猜你喜欢
随机推荐
bgp路由过滤
kubernetes
MySQL 视图(详解)
2022年SQL经典面试题总结(带解析)
JSESSIONID description in cookie
弹性盒子模型
opencv,numpy,tensor格式转换
字节对齐之C语言犄角旮旯的知识
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
JS中获取元素属性的8大方法
Flex布局详解
Why do so many people who teach themselves software testing give up later...
【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
2021年PHP-Laravel面试题问卷题 答案记录
Oracle ADG状态查看与相关视图
MYSQL JDBC图书管理系统
mysql8安装步骤教程
openim支持十万超级大群
GateWay实现负载均衡
go慢速入门——函数







![[Machine Learning] The Beauty of Mathematics Behind Gradient Descent](/img/63/c9d5d9370c28dbce0195e1ff26869b.jpg)

