当前位置:网站首页>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.
边栏推荐
猜你喜欢

LeetCode · 23. Merge K ascending linked lists · recursion · iteration

【深度学习】对迁移学习中域适应的理解和3种技术的介绍

MySQL 多表关联一对多查询实现取最新一条数据

KingbaseESV8R6 snapshot too old的配置和测试

C language: detailed explanation of operators

Babbitt | Metaverse Daily Must Read: The shuffling is coming, will the digital Tibetan industry usher in a new batch of leaders in the second half?Will there be new ways to play?...

Oblique document scanning and character recognition (opencv, coordinate transformation analysis)

Activiti 工作流引擎 详解

Image Restoration by Estimating Frequency Distribution of Local Patches

What is the common factor
随机推荐
canvas基础讲解加示例
Enhancing Quality for HEVC Compressed Videos
牛客小白月赛53 A-E
我是如何让公司后台管理系统焕然一新的(上) -性能优化
导航栏----个人中心 Dropdown
C语言中指针没那么难~(2)【文章结尾有资料】
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
JDBC (detailed explanation)
大家都在用的plm项目管理软件有哪些
MySQL group_concat()详解
LeetCode · 23. Merge K ascending linked lists · recursion · iteration
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
2021年软件测试面试题大全
数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)
awk笔记
[Deep Learning] Target Detection | SSD Principle and Implementation
mysql 递归函数with recursive的用法
MYSQL 唯一约束
Activiti 工作流引擎 详解
5分钟搞懂MySQL - 行转列