当前位置:网站首页>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.
边栏推荐
- 【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
- LeetCode · 23. Merge K ascending linked lists · recursion · iteration
- C语言中指针没那么难~ (1)【文章结尾有资料】
- DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
- 三层架构简单配置
- [Nuxt 3] (十四) Nuxt 生命周期
- Typescript 严格模式有多严格?
- Redis数据更新,是先更新数据库还是先更新缓存?
- 数据质量提升
- socket: Kernel initialization and detailed process of creating streams (files)
猜你喜欢

opencv,numpy,tensor格式转换

MySql 创建索引

mysql deadlock

LeetCode·每日一题·952.按公因数计算最大组件大小·并查集

GPGGA NTRIP RTCM Notes

5分钟搞懂MySQL - 行转列

MySQL 高级(进阶) SQL 语句 (一)

DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
![[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?](/img/6a/640763e3bd6c28f3e7fa762798b0ae.png)
[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?

外包干了三年,废了...
随机推荐
用于命名实体识别的模块化交互网络
HJ85 longest palindrome substring
How do I refresh the company's background management system (Part 1) - performance optimization
[Nuxt 3] (十三) Nuxt 是如何工作的?
对一次生产环境产生OOM的记录,结论:除非在自己完全有把握的情况下,否则不要偷懒查询无用字段
R package调试
mpls简介
深入浅出富文本编辑器
Redis数据更新,是先更新数据库还是先更新缓存?
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
@WebServlet注解(Servlet注解)
The structure of knowledge in the corners of the C language
MYSQL 唯一约束
flowable工作流所有业务概念
socket: Kernel initialization and detailed process of creating streams (files)
tcp协议传输中的粘包问题
拿什么来保护数据安全?基层数据安全体系建设待提升
mysql deadlock
IDEA2018.3.5 cancel double-click Shift shortcut
Typescript 严格模式有多严格?