当前位置:网站首页>Memory alignment of structure
Memory alignment of structure
2022-07-02 16:02:00 【Hero 2021】
List of articles
Memory alignment of structures is a Especially popular Knowledge points of !
Cited example :
#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;
}
Structure memory alignment rules
1. Structure of the The first variable , Always put it at the beginning of the structure The offset for the 0 The place of .
2. Structural members Start with the second member , The remaining members are always placed at an offset of an integer multiple of the alignment number .
Align numbers = The compiler's default alignment number is the smaller value of the variable's own size (VS The default number of alignments is 8,Linux There is no default alignment number )
3. The total size of the structure , It must be an integer multiple of the maximum number of alignments in each member of the structure
4. If nesting The situation of the structure , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
Meet the first point :
Meet the second point :
Meet the third step :
In order to meet the third point , We need memory alignment , The total size of the structure must be 4 Multiple , Now only 12 It can meet the requirements .
But we have some Waste of space ah !
So why have memory alignment ?
Most of the references say that :
- Platform reasons ( Reasons for transplantation ):
Not all hardware platforms can access any data on any address ; Some hardware platforms can only access certain types of data at certain addresses , Otherwise, a hardware exception will be thrown .- Performance reasons :
data structure ( Especially stacks ) It should be aligned as far as possible on the natural boundary .
The reason lies in , To access unaligned memory , The processor needs to make two memory accesses ; The aligned memory access only needs one access .
for example : Some scenes can only be read at a time 4byte Space : Compare alignment and misalignment , There may be errors when reading data .
On the whole :
The memory alignment of the structure is Trade space for time How to do it .
How to optimize ?
When designing structures , We have to satisfy the alignment , And save space , How to do it ? That is to let Members with small space shall be gathered together as much as possible .
// for example :
struct S1
{
char c1;
int i;
char c2;
};
struct S2
{
char c1;
char c2;
int i;
};
S1 and S2 The type of members as like as two peas , however S1 and S2 There are some differences in the amount of space taken up .
Change the default alignment number
use #pragma
Change the default alignment number
#include <stdio.h>
#pragma pack(8)// Set the default alignment number to 8
struct S1
{
char c1;
int i;
char c2;
};
#pragma pack()// Unset the default number of alignments , Restore to default
#pragma pack(1)// Set the default alignment number to 1
struct S2
{
char c1;
int i;
char c2;
};
#pragma pack()// Unset the default number of alignments , Restore to default
int main()
{
// What is the result of the output ?
printf("%d\n", sizeof(struct S1));
printf("%d\n", sizeof(struct S2));
return 0;
}
Conclusion :
When the structure is not aligned properly , We can change the default alignment number by ourselves .
边栏推荐
- Fiddler realizes mobile packet capturing - getting started
- SQL FOREIGN KEY
- Write your own CPU Chapter 11 - learning notes
- 如何实现十亿级离线 CSV 导入 Nebula Graph
- Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
- 愛可可AI前沿推介(7.2)
- Pyinstaller打包exe附带图片的方法
- Nebula Graph & 数仓血缘关系数据的存储与读写
- 爱可可AI前沿推介(7.2)
- 又是一年毕业季
猜你喜欢
智联招聘的基于 Nebula Graph 的推荐实践分享
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
idea jar包冲突排查
Add user-defined formula (time sharing t+0) to mobile app access as an example
Practice of constructing ten billion relationship knowledge map based on Nebula graph
Aiko ai Frontier promotion (7.2)
Experiment collection of University Course "Fundamentals of circuit analysis". Experiment 5 - Research on equivalent circuit of linear active two terminal network
如何實現十億級離線 CSV 導入 Nebula Graph
Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios
Write your own CPU Chapter 11 - learning notes
随机推荐
Boot 中bean配置覆盖
Song of cactus - throwing stones to ask the way (2)
智联招聘的基于 Nebula Graph 的推荐实践分享
结构体的内存对齐
《大学“电路分析基础”课程实验合集.实验五》丨线性有源二端网络等效电路的研究
SQL FOREIGN KEY
/Bin/ld: cannot find -lgssapi_ krb5
Review materials for the special topic of analog electronics with all essence: basic amplification circuit knowledge points
Flink real-time data warehouse (7): Flink realizes the full pull module to extract data in MySQL
Data Lake (11): Iceberg table data organization and query
图数据库|Nebula Graph v3.1.0 性能报告
《大学“电路分析基础”课程实验合集.实验六》丨典型信号的观察与测量
可视化技术在 Nebula Graph 中的应用
原神2.6服务端下载以及搭建安装教程
Application of visualization technology in Nebula graph
How to use percona tool to add fields to MySQL table after interruption
数据库系统概论第一章简答题-期末考得怎么样?
Nebula Graph & 数仓血缘关系数据的存储与读写
Group by的用法
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics