当前位置:网站首页>Find the memory occupied by the structure
Find the memory occupied by the structure
2022-07-28 20:01:00 【Hello_ World_ two hundred and thirteen】
Find the space occupied by a certain type , We need to use sizeof keyword
for example :sizeof(int)
sizeof(char)
sizeof(double)
So if sizeof Inside the bracket is a structure ? A structure is a collection of many types , How can we find the space it occupies ?
There is a point of knowledge here : Structure memory alignment
1. first Members are related to structural variables The offset for the 0 The address of
2. Other member variables should be aligned to a An integer multiple of the alignment number The address of
Align numbers = Compiler default alignment number And The size of the member Smaller value
vs The default value in is 8
Set the default number of alignments :
#pragma pack(6) // Set the default alignment number to 6
#pragma pack(0)//0 Invalid value , The minimum is 1
3. structure Total body size yes Maximum number of alignments ( Each member variable has an alignment number ) Of An integral multiple .
4. If the structure is nested , 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
Example :
struct S1
{
char c1;
int i;
char c2;
};
sizeof(struct S1); --- 12
Why is there memory alignment ?
1. 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
2. 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 do two memory accesses ;
The aligned memory access only needs one access
3. On the whole :
The memory alignment of the structure is Space In exchange for Time How to do it
How to save space , It saves time ?
The answer is : Let the members who occupy less space gather together as much as possible
for example , For the example above , We make the following modifications
struct S1
{
char c1;
char c2;
int i
};
边栏推荐
- What is the process of swing event processing?
- editor.md中markdown编辑器的实现
- Integration and implementation of login click graphic verification code in personal blog system
- Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
- “中国网事·感动2022”二季度网络感动人物评选结果揭晓
- Leetcode Day5 delete duplicate email
- [网络]跨区域网络的通信学习路由表的工作原理
- 软考中级(系统集成项目管理工程师)高频考点
- Leetcode Day2 consecutive numbers
- 基于QTGUI图像界面的空战游戏设计
猜你喜欢

河北邯郸:拓展基层就业空间 助力高校毕业生就业

河北:稳粮扩豆助力粮油生产提质增效

Source insight project import and use tutorial

Codeignier framework implements restful API interface programming

Design of air combat game based on qtgui image interface

Cloud computing notes part.2 - Application Management

Common APIs in string

利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)

Basic concept and essence of Architecture

云计算笔记part.1——系统管理
随机推荐
并发程序设计,你真的懂吗?
Labelme (I)
Leetcode day3 employees who exceed the manager's income
Idea properties file display \u solution of not displaying Chinese
Oracle insert数据时字符串中有‘单引号问题
MySQL performance testing tool sysbench learning
Why is there no log output in the telnet login interface?
Function fitting based on MATLAB
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Source insight project import and use tutorial
MySQL性能测试工具sysbench学习
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Theoretical knowledge of digital image (I) (personal analysis)
Integration and implementation of login click graphic verification code in personal blog system
The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
Machine learning -- model evaluation, selection and verification
中国能否在元宇宙的未来发展中取得突破,占领高地?
leetcode day4 部门工资最高的员工
editor.md中markdown编辑器的实现
Array method added in ES6