当前位置:网站首页>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
};
边栏推荐
- [network] cross area network communication learning classification and calculation of IPv4 address
- Left alignment function of Lua language (handwriting)
- Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
- 云计算笔记part.1——系统管理
- The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
- Codeignier framework implements restful API interface programming
- 利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
- leetcode day3 超过经理收入的员工
- Cloud computing notes part.2 - Application Management
- Andorid system layout, values, drawable adaptation
猜你喜欢

Business visualization - let your flowchart "run" (4. Actual business scenario test)

Function fitting based on MATLAB

克服“看牙恐惧”,我们用技术改变行业

时间转日期的sql语句应该怎么写?

CodeIgnier框架实现restful API接口编程

Servlet学习笔记

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency

C language pointer and two-dimensional array

Intermediate soft test (system integration project management engineer) high frequency test site

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
随机推荐
Basic concept and essence of Architecture
NetCoreAPI操作Excel表格
MySQL性能测试工具sysbench学习
English Translation Spanish - batch English Translation Spanish tools free of charge
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
How many types of rain do you know?
How to write the SQL statement of time to date?
[in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
Saltstack advanced
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
你知道雨的类型有几种?
This customized keyboard turns me on~
How does app automated testing achieve H5 testing
Integration and implementation of login click graphic verification code in personal blog system
MySQL8 Encrypting InnoDB Tablespaces
Function fitting based on MATLAB
11. Learn MySQL union operator
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
How does app automated testing achieve H5 testing
数字图像理论知识(一)(个人浅析)