当前位置:网站首页>What is stack and the difference between stacks
What is stack and the difference between stacks
2022-07-23 18:56:00 【Programmer Huazai】
This article was first published on the official account 【 Programmer Huazai 】
------------------
First of all, let's explain , Heaps and stacks discussed here , It refers to “ Heap area ” and “ The stack area ”, It is not the heap and stack mentioned in the data structure .
To understand the stack in program memory , You need to know C Memory model of language .
C The memory model of language is divided into 5 Districts : Code section , Global area , The constant area , Stack area and stack area .
1. Code section
Store binary code area , stay ELF called .text paragraph .
2. Global area
Area for storing global and static variables .
The initialized global and static variables are in an area of the global , stay ELF Named as .data paragraph .
Uninitialized global variables and static variables are in the adjacent area , stay ELF In Chinese, it means .bss paragraph .
The memory of the global area is allocated by the system , And at the end of the program , Released by the system .
3. The constant area
The area where constants are stored . No modification allowed .
The memory of this area is at the end of the program , Released by the system .
4. Heap area (Heap)
An area for storing variables created by programmers , It is a discontinuous area , In general use new,alloc,calloc,malloc Keyword to create variables , These variables need to be called by the programmer delete,release,free Keywords can be released . If there is no call , It will cause a memory leak .
5. The stack area (Stack)
Store the parameters of the function , Region of local variables , Automatically allocated and released by the compiler . Variables are usually released outside the scope . Formally, it is similar to stack in data structure . Because in CPU Allocate memory in the instruction set of , So the operation is fast and efficient . But the space is very limited , for example iOS The stack size of is 2M.
Heap and stack concepts
Pile up It is a large discontinuous area , Generally, the linked list in the data structure is used to organize and manage , In the process of distribution , It is easy to produce memory fragments .
Stack It's a small continuous area , Generally, the queue is used according to the first in and last out (First In Last Out)(FILO) Sort in a different way , And only at one end ( To the top of the stack (top)) Insert and delete data .
The difference between heap and stack
Heap and stack , There are two different ways for the operating system to manage the memory space of the process , There are mainly the following differences :
(1) Different ways of distribution :
Stack : Automatically assigned and released by the operating system , No need for us to operate it manually . The allocation method is similar to the stack in the data structure .
Pile up : The application and release work is completed by the programmer , If programmers don't release , Memory leaks occur . The allocation method is similar to the linked list in the data structure .
(2) The size of the space is different :
Stack : Stack space is much smaller than heap space . such as iOS Stack as 2M,64 Bit Linux Default 10MB.
Pile up : Theoretically , The heap size that the process can apply for is the size of virtual memory .
(3) Different storage contents :
Stack : Store function return address 、 Parameters 、 Local variables and registers .
Pile up : The specific storage content is filled in by the programmer .
(4) Different caching methods :
Stack : Stored in L1 cache , Put it into storage space when called , Release immediately after the call .
Pile up : Stored in L2 cache , Reference counting is generally used (iOS) Conduct management .
(5) The data structure is different :
Pile up : Generally, the tree in the data structure is used to manage .
Stack : Generally, the queue of first in and last out is used to manage .
(6) Distribution efficiency is different :
The stack is automatically allocated by the operating system , stay CPU The instruction set of completes the allocation and is supported by the hardware layer ( Special registers are responsible for storage , Special instructions are responsible for pressing the stack ).
Heap is made up of system kernel API Interface to complete application and management , The implementation mechanism is complex , And frequent memory applications are prone to memory fragmentation . In this way, the allocation efficiency of stack is much higher than that of heap .
These are the six differences of stack .
iOS App Memory layout
So let's see iOS App Memory layout , Shown below .

Just like the above figure ,
1. In the whole memory space , The top is the high address , At the bottom is the low address ;
2. The code segment and data segment are in the lowest address bit , That is, the lowest end ;
3. On top of it is a heap , It's used to store variables ; about ObjC Come on , Namely new,alloc,calloc,malloc Keyword modifies the variable , The address expansion mode of the heap is from low to high , That is, when it is not enough, apply for a space address up
4. Up is the stack , It mainly stores the parameters of the function 、 Data such as local variables , Its expansion mode is from high to low .
That's all iOS App Memory layout of , Understand and master iOS App Memory layout of , Especially stack allocation and expansion trend , It can help us analyze the positioning problem well .
边栏推荐
- [attack and defense world web] difficulty four-star 12 point advanced question: cat
- PCL: ajustement multiligne (RANSAC)
- How does the NiO mechanism of jetty server cause out of heap memory overflow
- deepstream学习笔记(二):gstreamer与deepstream-test1说明
- 基于 softether 搭建虚拟私有网络
- Flink Exactly-Once 投递实现浅析
- quota的使用方法
- Know two things: how does redis realize inventory deduction and prevent oversold?
- moxa串口服务器型号,moxa串口服务器产品配置说明
- [2020] [paper notes] optically controlled spectral ratio adjustable y based on two-dimensional photonic crystal——
猜你喜欢

【3D建模制作技巧分享】Zbrush如何将图片转浮雕模型
![Log framework [detailed learning]](/img/2f/2aba5d48e8a544eae0df763d458e84.png)
Log framework [detailed learning]

Rhcsa notes 7

【2022】【论文笔记】太赫兹量子阱——
![[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz](/img/df/bc757c7f6f6b801fafdd5a99352ddd.png)
[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz

【攻防世界WEB】难度三星9分入门题(终):fakebook、favorite_number

【2020】【论文笔记】基于Rydberg原子的——

MQ【MessageQueue 图文详解及四大MQ比较】

【2020】【论文笔记】太赫兹新型探测——太赫兹特性介绍、各种太赫兹探测器
![[heavyweight] focusing on the terminal business of securities companies, Borui data released a new generation of observable platform for the core business experience of securities companies' terminals](/img/28/8d9f33ad6f54d6344429a687a7d1d7.png)
[heavyweight] focusing on the terminal business of securities companies, Borui data released a new generation of observable platform for the core business experience of securities companies' terminals
随机推荐
80+嘉宾登台,10余国用户参会,7万+观众收看,「GWEI 2022-新加坡」落幕
作为3D资深建模师给刚入门学习伙伴的一些建议,用起来
Jumpserver administrator account is locked
[2020] [paper notes] Based on Rydberg atom——
[2020] [paper notes] phase change materials and Hypersurfaces——
[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz
有人是靠自学建模找到工作的吗?千万别让这些思维害了你
Storage structure and method of graph (II)
JUC concurrent programming [detailed explanation and demonstration]
一文了解 NebulaGraph 上的 Spark 项目
【2022】【论文笔记】太赫兹量子阱——
An SQL question about grouping query
1259. 不相交的握手 動態規劃
Gradle【图文安装及使用演示 精讲】
deepstream学习笔记(二):gstreamer与deepstream-test1说明
[2018] [paper notes] graphene FET and [2] - Preparation and transfer of graphene
并非原创的原文路径【如有侵权 请原博主联系删除】
Have a safe summer vacation, no holidays! Please keep these summer safety tips
【攻防世界WEB】难度四星12分进阶题:Cat
JUC并发编程【详解及演示】