当前位置:网站首页>结构体的内存对齐
结构体的内存对齐
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;
}
结论:
结构在对齐方式不合适的时候,我么可以自己更改默认对齐数。
边栏推荐
- Multi data source configuration code
- Ant group's large-scale map computing system tugraph passed the national evaluation
- 《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
- Pyinstaller打包exe附带图片的方法
- Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
- Idea jar package conflict troubleshooting
- 《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
- 全方位解读服务网格(Service Mesh)的背景和概念
- Flink real-time data warehouse (IX): incremental synchronization of data in MySQL
- Moveit 避障路径规划 demo
猜你喜欢
Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin
End time processing method of wechat v3native payment settings
Xpt2046 four wire resistive touch screen
Soul torture, what is AQS???
Another graduation season
[5g NR] RRC connection release
Ant group's large-scale map computing system tugraph passed the national evaluation
数仓中的维度表与事实表
Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios
The sea of stars hidden behind the nebula graph
随机推荐
Construction and business practice of Zhongke brain knowledge map platform
fastjson List转JSONArray以及JSONArray转List「建议收藏」
Postgressql stream replication active / standby switchover primary database no read / write downtime scenario
智联招聘的基于 Nebula Graph 的推荐实践分享
6092. Replace elements in the array
Boot connection to impala database
Flink real-time data warehouse (7): Flink realizes the full pull module to extract data in MySQL
/bin/ld: 找不到 -lxml2
Strings and arrays
/Bin/ld: cannot find -lxml2
How to import a billion level offline CSV into Nepal graph
lseek 出错
Ant group's large-scale map computing system tugraph passed the national evaluation
PyObject 转 char* (string)
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
图数据库|Nebula Graph v3.1.0 性能报告
源码look me
Demo of converting point cloud coordinates to world coordinates
Golang MD5 encryption and MD5 salt value encryption
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network