当前位置:网站首页>Structure size
Structure size
2022-07-25 14:14:00 【IM-STONE】
1、 The size of the structure is formed by adding the member variables and offsets ;
2、 Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of , The overall size should also be divisible by the aligned number
Total alignment : = The compiler defaults to an alignment number and the smaller value of the member size , That is to say min{ The compiler's default number of alignments , The maximum size of the member variable },VS The default alignment number in is 8;
Number of individual alignments : The memory occupied by variables of this type ,char The alignment number is 1 ,int The alignment number is 4 , double yes 8 , 32 The bit pointer type is 4 , 64 The bit pointer type is 8;
The alignment principle is divided into two steps :
1、 Single member variable alignment ( in other words , The starting address of a single variable can be divided by a single aligned number )
2、 Overall alignment ( The overall size can be divided by the aligned number , If it can't be divided completely , Then increase the offset to fill )
Same member variable , Because the declaration position is different , As a result, the occupied memory space is different , Look at the code below
void test6()
{
// Total alignment min{ The compiler's default number of alignments =8 , The maximum size of the member variable double=8 } = 8
struct node
{
// Suppose the total space is all
double m;//8 double The alignment number is 8 , here all=8 [0:7]
int a;//4 int The alignment number is 4 all=12 [8:11]
char c;//1 char Align numbers 1 all=13 [12]
char b;//1 char Align numbers 1 all=14 [13]
};//16 ** Start overall alignment , It should be divisible by the whole alignment number , That is, it can be 8 to be divisible by ,14 Can not be 8 to be divisible by ,+2=16 that will do , therefore all=16**
struct node1
{
char c; //1 char Align numbers 1 all=1
int a;//4 int Align numbers 4 (char c Three bytes are left after , Store from the fourth byte int a) all=8
char b; //1 char Align numbers 1 all=9
double m;//8 double The alignment number is 8 (char b Rear empty 7 Bytes , Store from the fourth byte double m) , here all=24
};//24 24 Can be 8 to be divisible by , So the whole does not need to be offset
cout << sizeof(node) << "---" << sizeof(node1) << endl;
}
Single member variable alignment ( in other words , The starting address of a single variable can be divided by a single aligned number ) More generally speaking, it is ;double Type must be from 8 The divisible address begins ,int Must be 4 The divisible address begins , If the starting address cannot be divisible , Then move backward , Until it can be divided , The empty space is directly wasted , Trade space for time .
If the structure is nested , The inner nested structure member is aligned to the address of an integer multiple of its maximum alignment number , The overall size of the structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
Why offset
The reason for the platform ( Reasons for transplantation ): Not all hardware platforms can access any data on any address ; Some hardware platforms can only get certain types of data at certain addresses , Otherwise, a hardware exception will be thrown .
Performance reasons : data structure ( Especially stacks ) It should be aligned on the natural boundary as much as possible . The reason lies in , To access unaligned memory , The processor needs to make two memory accesses ; The aligned memory access only needs one access .
边栏推荐
- OKA通证权益解析,参与Okaleido生态建设的不二之选
- Mongodb source code deployment and configuration
- Keys and scan based on redis delete keys with TTL -1
- CDA level Ⅰ 2021 new version simulation question 2 (with answers)
- DVWA practice - brute force cracking
- Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder
- Esp32 connects to Alibaba cloud mqtt IOT platform
- maya建模练习
- From Anaconda to tensorflow to jupyter, step on the pit and fill it all the way
- Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
猜你喜欢

Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO

Gartner 2022 top technology trend: Super automation

Brush questions - Luogu -p1075 prime factor decomposition

安防市场进入万亿时代,安防B2B网上商城平台精准对接深化企业发展路径

元器件采购系统的主要功能,数字化采购助力元器件企业飞速发展

如何设计一个高并发系统?
![[原创]九点标定工具之机械手头部相机标定](/img/de/5ea86a01f1a714462b52496e2869d6.png)
[原创]九点标定工具之机械手头部相机标定

机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶

Advantages of wireless relay acquisition instrument and wireless network for engineering monitoring

Engineering monitoring multi-channel vibrating wire sensor wireless acquisition instrument external digital sensor process
随机推荐
maya建模练习
Gartner 2022 top technology trend: Super automation
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster
It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
Apple failed to synchronize on its mobile terminal, and logged out. As a result, it could not log in again
Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
IDEA设置提交SVN时忽略文件配置
Idea regular expression replacement (idea regular search)
Lesson of C function without brackets
Tm1637 four digit LED display module Arduino driver with second dot
Maya modeling exercise
Summary of some problems about left value and right value [easy to understand]
Numpy basic package for data analysis
【学习记录】plt.show()闪退解决方法
Typora无法打开提示安装新版本解决办法
Write an esp32 Watchdog with Arduino
Business analysis report and data visualization report of CDA level1 knowledge point summary
Tm1638 LED digital display module Arduino drive code
einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform