当前位置:网站首页>结构体的内存对齐
结构体的内存对齐
2022-07-02 12:39:00 【Hero 2021】
结构体的内存对齐是一个 特别热门的知识点!
引例:
#include<iostream>
using namespace std;
struct S
{
char c; // 1
int a; // 4
char d; // 1
};
int main()
{
struct S s = {
'a',2,'y'};
cout << sizeof(struct S) << endl;// 12
cout << sizeof(s) << endl; // 12
return 0;
}
结构体内存对齐规则
1. 结构体的第一个变量,永远放在结构体起始位置偏移量为0的地方。
2. 结构体成员从第二个成员开始,剩下的成员总是放在偏移量为一个对齐数的整数倍处。
对齐数=编译器默认的对齐数与变量自身大小的较小值(VS的默认对齐数是8,Linux没有默认对齐数)
3. 结构体的总大小,必须是结构体各个成员中最大对齐数的整数倍
4. 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数( 含嵌套结构体的对齐数)的整数倍。
满足第一点:
满足第二点:

满足第三步:

为了满足第三点,我们要内存对齐,结构体的总大小必须是4的倍数,现在只有12能够满足要求了。
但是我们存在着一些空间的浪费啊!
那么为什么要有内存对齐呢?
大部分的参考资料都是如是说的:
- 平台原因(移植原因):
不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。- 性能原因:
数据结构(尤其是栈)应该尽可能地在自然边界上对齐。
原因在于,为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。
例如:某些场景下一次只能读取4byte空间:对比对齐和不对齐,可能读取数据就可能出错。
总体来说:
结构体的内存对齐是拿空间来换取时间的做法。
如何优化?
那在设计结构体的时候,我们既要满足对齐,又要节省空间,如何做到呢? 那就是让占用空间小的成员尽量集中在一起。
//例如:
struct S1
{
char c1;
int i;
char c2;
};
struct S2
{
char c1;
char c2;
int i;
};
S1和S2类型的成员一模一样,但是S1和S2所占空间的大小有了一些区别。
修改默认对齐数
用#pragma修改默认对齐数
#include <stdio.h>
#pragma pack(8)//设置默认对齐数为8
struct S1
{
char c1;
int i;
char c2;
};
#pragma pack()//取消设置的默认对齐数,还原为默认
#pragma pack(1)//设置默认对齐数为1
struct S2
{
char c1;
int i;
char c2;
};
#pragma pack()//取消设置的默认对齐数,还原为默认
int main()
{
//输出的结果是什么?
printf("%d\n", sizeof(struct S1));
printf("%d\n", sizeof(struct S2));
return 0;
}
结论:
结构在对齐方式不合适的时候,我么可以自己更改默认对齐数。
边栏推荐
- Boot 中bean配置覆盖
- Data Lake (11): Iceberg table data organization and query
- Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin
- (万字精华知识总结)Shell脚本编程基础知识
- [solution] educational codeforces round 82
- 隐藏在 Nebula Graph 背后的星辰大海
- Solve the problem of base64encoder error
- Introduction to dynamic planning I, BFS of queue (70.121.279.200)
- Teach you how to build virtual machines locally and deploy microservices
- Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
猜你喜欢

Aiko ai Frontier promotion (7.2)

Pattern matching extraction of specific subgraphs in graphx graph Computing Practice

又是一年毕业季

Recommended practice sharing of Zhilian recruitment based on Nebula graph

End time processing method of wechat v3native payment settings

Experiment collection of University "Fundamentals of circuit analysis". Experiment 7 - Research on sinusoidal steady-state circuit
![[5g NR] RRC connection release](/img/f3/a03f5124493b1c03e7336c55871330.png)
[5g NR] RRC connection release

After the win10 system is upgraded for a period of time, the memory occupation is too high

Xpt2046 four wire resistive touch screen

Aike AI frontier promotion (7.2)
随机推荐
Nebula Graph & 数仓血缘关系数据的存储与读写
In memory of becoming the first dayu200 tripartite demo contributor
/Bin/ld: cannot find -lpam
idea jar包冲突排查
Some problems about pytorch extension
matlab中wavedec2,说说wavedec2函数[通俗易懂]
Application of visualization technology in Nebula graph
(万字精华知识总结)Shell脚本编程基础知识
What are the necessary functions of short video app development?
beforeEach
Use ffmpeg command line to push UDP and RTP streams (H264 and TS), and ffplay receives
爱可可AI前沿推介(7.2)
Pyinstaller's method of packaging pictures attached to exe
Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals
Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
Fiddler实现手机抓包——入门
SQL FOREIGN KEY
Target detection - make your own deep learning target detection data set with labelimg
floyed「建议收藏」
(5) Flink's table API and SQL update mode and Kafka connector case