当前位置:网站首页>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); //6
struct 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.
边栏推荐
猜你喜欢
IDEA2018.3.5取消双击Shift快捷键
KingbaseESV8R6 snapshot too old的配置和测试
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
你需要知道的ES6—ES13开发技巧
MySQL 视图(详解)
Automatically generate test modules using JUnit4 and JUnitGenerator V2.0 in IDEA
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
MySQL ODBC驱动简介
【限时福利】21天学习挑战赛 - MySQL从入门到精通
flyway的快速入门教程
随机推荐
(7/29)基础板子最小生成树prim+kruskal
Enhancing Quality for HEVC Compressed Videos
GPGGA NTRIP RTCM 笔记
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-
Swift RegexBuilder Vs. Raku Grammar
【限时福利】21天学习挑战赛 - MySQL从入门到精通
Mysql 回表
MySQL 多表关联一对多查询实现取最新一条数据
弹性盒子模型
深入浅出富文本编辑器
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
5分钟搞懂MySQL - 行转列
bebel系列- 插件开发
MYSQL 唯一约束
数据指标口径不统一、重复开发?亿信ABI指标管理平台帮你解决
拿什么来保护数据安全?基层数据安全体系建设待提升
MySQL ODBC驱动简介
MySQL BIGINT 数据类型
深入浅出边缘云 | 3. 资源配置
ELF:加载过程