当前位置:网站首页>结构体大小
结构体大小
2022-07-25 14:12:00 【IM-STONE】
1、结构体大小由成员变量和偏移量相加而成;
2、其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处,整体大小也要能被对齐数整除
总对齐数: = 编译器默认的一个对齐数与该成员大小的较小值, 也就是min{ 编译器默认的对齐数, 成员变量大小的最大值},VS中默认的对齐数为8;
单个对齐数:该类型变量所占内存大小,char 对齐数是1 ,int 对齐数是4 , double 是8 , 32位指针类型是4 , 64位指针类型是8;
对齐原则分两步:
1、单个成员变量对齐(也就是说,单个变量的起始地址能被单个对齐数整除)
2、整体对齐(整体大小能被对齐数整除,若不能不整除,则增加偏移来填充)
相同的成员变量,因为声明位置不同,导致占用内存空间不同,看下面代码
void test6()
{
//总对齐数min{编译器默认的对齐数=8 ,成员变量大小的最大值double=8 } = 8
struct node
{
//假设总体空间为all
double m;//8 double对齐数为8 ,此时all=8 [0:7]
int a;//4 int 对齐数为 4 all=12 [8:11]
char c;//1 char对齐数 1 all=13 [12]
char b;//1 char对齐数 1 all=14 [13]
};//16 **开始整体对齐,要能被整体对齐数整除,也就是能被8整除,14不能被8整除,+2=16 即可,所以all=16**
struct node1
{
char c; //1 char对齐数 1 all=1
int a;//4 int对齐数 4 (char c 后空出三个字节,从第四个字节开始存放 int a) all=8
char b; //1 char对齐数 1 all=9
double m;//8 double对齐数为8 (char b 后空出7个字节,从第四个字节开始存放double m) ,此时all=24
};//24 24能被8整除,所以整体不需要偏移
cout << sizeof(node) << "---" << sizeof(node1) << endl;
}
单个成员变量对齐(也就是说,单个变量的起始地址能被单个对齐数整除)更加通俗的讲就是;double类型必须从被8整除的地址开始,int 必须从被4整除的地址开始,若起始地址不能被整除,则向后移动,直到能被整除,空出来的空间直接浪费,用空间换时间。
如果嵌套了结构体的情况,内层嵌套的结构体成员对齐到自己的最大对齐数的整数倍的地址,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。
为什么要偏移
平台的原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取到某些特定类型的数据,否则抛出硬件异常。
性能的原因: 数据结构(尤其是栈)应该尽可能的在自然边界上对齐。原因在于,为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。
边栏推荐
- It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
- What you must know about data engineering in mlops
- Oka pass rights and interests analysis is the best choice to participate in okaleido ecological construction
- Typora无法打开提示安装新版本解决办法
- 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)
- PHP website design ideas
- Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform
- How happy is the frisbee bureau? 2022 youth trendy Sports Report
- telnet远程登录aaa模式详解【华为eNSP】
- Teach you how to apply for SSL certificate
猜你喜欢

手把手教你申请SSL证书

Brush questions - Luogu -p1047 trees outside the school gate

OKA通证权益解析,参与Okaleido生态建设的不二之选

Esp32 connects to Alibaba cloud mqtt IOT platform

Brush questions - Luogu -p1161 turn on the light

Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt

Pytorch uses tensorboard to realize visual summary

Brush questions - Luogu -p1059 clear random number

From Anaconda to tensorflow to jupyter, step on the pit and fill it all the way

实现一个家庭安防与环境监测系统(二)
随机推荐
Common problems of wireless relay acquisition instrument
CDA level1 multi topic selection
Okaleido生态核心权益OKA,尽在聚变Mining模式
Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder
Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
基于redis的keys、scan删除ttl为-1的key
sqli-labs Basic Challenges Less1-10
maya建模练习
Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO
Wangeditor rich text editor
Apple failed to synchronize on its mobile terminal, and logged out. As a result, it could not log in again
Brush questions - Luogu -p1059 clear random number
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 2) -- use of openmmlab framework
[原创]九点标定工具之机械手头部相机标定
IDEA设置提交SVN时忽略文件配置
金鱼哥RHCA回忆录:CL210管理存储--对象存储
Application engineering safety monitoring of wireless vibrating wire acquisition instrument
RuntimeError: CUDA out of memory(已解决)[通俗易懂]
Brush questions - Luogu -p1151 sub number integer