当前位置:网站首页>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 .
边栏推荐
- Leetcode 0131. split palindrome string
- 【2022】【论文笔记】太赫兹量子阱——
- [2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz
- Redis【超强超细 入门教程】
- How does Apache, the world's largest open source foundation, work?
- 次世代行业现状如何?90%转行建模师都在学习这套流程
- Is it suitable for learning 3D modeling? You can't lose one of these five points
- Multithreading [comprehensive study of graphics and text]
- OSI模型第一层:物理层,基石般的存在!
- 80 + guests took the stage, users from more than 10 countries attended the meeting, and 70000 + viewers watched the end of "Gwei 2022 Singapore"
猜你喜欢

FPGA实现IIC协议(二)之IIC总线的FPGA实现(单次读写驱动)
![Multithreading [comprehensive study of graphics and text]](/img/70/8a1227b2159349cf25a85dff8f9d1c.png)
Multithreading [comprehensive study of graphics and text]

Deepstream learning notes (II): description of GStreamer and deepstream-test1

什么是堆栈以及堆栈的区别

Storage structure and method of graph (II)

图的存储结构与方法(二)

类的基础

多线程【全面学习 图文精讲】

Building virtual private network based on softther

使用kail破解wifi密码
随机推荐
? The problem of front desk parameter transmission needs to be confirmed
Google正在改进所有产品中的肤色表现 践行“图像公平”理念
SQL 语句练习
Paddlenlp's UIE classification model [taking emotional propensity analysis news classification as an example] including intelligent annotation scheme)
398. Random number index hash table method
LeetCode 剑指 Offer II 115.重建序列:图解 - 拓扑排序
An SQL question about grouping query
Great God "magic change" airpods, equipped with usb-c interface, 3D printing shell makes maintenance easier
【攻防世界WEB】难度三星9分入门题(终):fakebook、favorite_number
Completion report of communication software development and Application
识别引擎ocropy->ocropy2->OCRopus3总结
Multithreading [comprehensive study of graphics and text]
Learn about spark project on nebulagraph
【游戏建模模型制作全流程】3ds Max和ZBrush制作无线电接收器
Building virtual private network based on softther
Storage structure and method of graph (II)
Does anyone get a job by self-study modeling? Don't let these thoughts hurt you
次世代行业现状如何?90%转行建模师都在学习这套流程
mysql的访问端口是什么意思_数据库端口是什么端口号
入行3D建模有前景吗?就业高薪有保障还是副业接单赚钱多