当前位置:网站首页>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 .
边栏推荐
- Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
- Detailed explanation of nat/napt address translation (internal and external network communication) technology [Huawei ENSP]
- 苹果手机端同步不成功,退出登录,结果再也登录不了
- CTS test introduction (how to introduce interface test in interview)
- 华为ensp路由器静态路由(默认路由的下一跳地址)
- Esp32 connects to Alibaba cloud mqtt IOT platform
- bond0脚本
- Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder
- From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part I)
- Working principle of Lora to 4G and gateway repeater
猜你喜欢

Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders

Business analysis report and data visualization report of CDA level1 knowledge point summary

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

如何让一套代码完美适配各种屏幕?

maya建模练习

Idea settings ignore file configuration when submitting SVN

Brush questions - Luogu -p1146 coin flip

Keys and scan based on redis delete keys with TTL -1

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

Doris学习笔记之与其他系统集成
随机推荐
Leetcode 205. isomorphic string (2022.07.24)
sqli-labs Basic Challenges Less11-22
Realize a family security and environmental monitoring system (II)
Brush questions - Luogu -p1146 coin flip
~5 new solution of CCF 2021-12-2 sequence query
Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
知名手写笔记软件 招 CTO·坐标深圳
Advantages of wireless relay acquisition instrument and wireless network for engineering monitoring
Sqli labs installation environment: ubuntu18 php7
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster
Business data analysis of CDA level1 knowledge point summary
Lesson of C function without brackets
swiper 一侧或两侧露出一小部分
[original] nine point calibration tool for robot head camera calibration
Esp32 connects to Alibaba cloud mqtt IOT platform
力扣(LeetCode)205. 同构字符串(2022.07.24)
数字孪生 - 认知篇
飞沃科技IPO过会:年营收11.3亿 湖南文旅与沅澧投资是股东
The practice of depth estimation self-monitoring model monodepth2 in its own data set -- single card / multi card training, reasoning, onnx transformation and quantitative index evaluation
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis