当前位置:网站首页>About stack area, heap area, global area, text constant area and program code area
About stack area, heap area, global area, text constant area and program code area
2022-07-04 22:30:00 【InfoQ】
One by C/C++ The memory occupied by the compiled program is divided into the following parts
The stack area (
stack
)
Heap area (
heap
)
Global area ( Static zone )(
static
)
- Initialized global and static variables in one area ,
- Uninitialized global variables and uninitialized static variables are in another adjacent area .
Text constant area
Program code area
int a = 0; // Global initialization area
char *p; // Global uninitialized area
int main(int argc, char * argv[]) {
int b; // Stack
char *p1; // Stack
char s[] = "abc"; // Stack
char *p2 = "123456"; //"123456" In the constant area ,p2 On the stack
static int c = 0; // Global static area , Initialization area
p = (char *)malloc(10); // It's distributed 10 and 20 The byte area is in the heap area
p1 = (char *)malloc(20); // It's distributed 10 and 20 The byte area is in the heap area
strcpy(p1, "123456"); //123456 In the constant area , The compiler may associate it with p1 Points to 123456 Optimize into one place
}
Theoretical knowledge of heap and stack
Application method
- Stacks are allocated automatically by the system , Faster , It's a continuous memory area , Size is 2MB. for example , Declare a local variable in a function
int b
; The system automatically addsb
Open up space
- Heap requires programmers to apply , And indicate the size , Relatively slow . use
malloc
,alloc
,new
,calloc
,realloc
Wait for the function that allocates memory to get on the heap . in addition , Heap is a data structure extended to high address , It's a discontinuous memory area , The size of the heap is limited by the virtual memory of the computer . Therefore, the acquisition and use of heap space is more flexible , Large available space . for example
p1 = (char *)malloc(10);
p2 = (char *)malloc(10);
But notice p1、p2 Itself is in the stack .
System response after application
- Stack : As long as the remaining space of the stack is larger than the requested space , The system will provide memory for the program , Otherwise, an exception will be reported indicating that the stack overflows .
- Pile up : First of all, you should know that the operating system has a list of free memory addresses , When the system receives an application for a program , Will traverse the list , Find the first heap node whose space is larger than the applied space , Then delete the node from the free node list , And allocate the space of the node to the program , in addition , For most systems , The size of this allocation will be recorded at the first address in this memory space , such , In code
delete
Statement can correctly release the memory space . in addition , Because the size of the heap node found is not necessarily exactly the size of the request , The system will automatically put the extra part back into the free list .
Request size limit
- Stack : stay Windows Next , Stacks are data structures that extend to low addresses , It's a continuous area of memory . The address at the top of the stack and the maximum capacity of the stack are predetermined by the system , stay Windows Next , The size of the stack is 2M( Some say it is 1M, All in all, it's a constant determined at compile time ), If the requested space exceeds the remaining space of the stack , Will prompt
overflow
. therefore , The space available from the stack is small .
- Pile up : Heap is a data structure extended to high address , It's a discontinuous memory area . This is because the system uses linked list to store the free memory address , Nature is not continuous , The traversal direction of the list is from low address to high address . The size of the heap is limited by the virtual memory available in the computer system . thus it can be seen , The heap gets more flexible space , It's bigger .
Comparison of application efficiency
- Stacks are allocated automatically by the system , Faster . But programmers can't control it .
- The heap is made up of
new
Allocated memory , Generally speaking, it's slow , And it's prone to memory fragmentation , But it's the most convenient . in addition , stay Windows Next , The best way isVirtualAlloc
Allocate memory , He's not piling up , It's not in the stack, it's in the address space of the process , Although it's the most inconvenient to use . But fast , And the most flexible .
Heap and stack storage
- Stack : On function call , The first one on the stack is the next instruction in the main function ( The next executable statement of a function call statement ) The address of , And then the parameters of the function , In most of C In the compiler , Parameters are pushed from right to left , And then the local variables in the function . Note that static variables are not pushed . When this function call ends , Local variables first out of stack , And then the parameters , Finally, the top of the stack pointer points to the address of the first storage , That is, the next instruction in the main function , The program continues to run from this point .
- Pile up : Generally, a byte is used to store the size of the heap in the head of the heap . The specific contents of the heap are arranged by the programmer .
Access efficiency comparison
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa It's assigned at runtime ;
and bbbbbbbbbbb It's determined at compile time ;
#include
void main(){
char a = 1;
char c[] = "1234567890";
char *p ="1234567890";
a = c[1];
a = p[1];
return;
}
10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],
cl11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]00401070 8A 42 01 mov al,
byte ptr [edx+1]00401073 88 45 FC mov byte ptr [ebp-4],al
cl
edx
edx
Summary ( The difference between heap and stack can be seen in the following metaphor )
- Using a stack is like eating in a restaurant , Just order ( Issue an application )、 pay 、 And eating ( Use ), When you are full, go , Don't pay attention to cutting vegetables 、 Prepare for dishes and dishes 、 Brush the pot and finish the work , His advantage is fast , But the freedom is small .
- Using heaps is like making your own favorite dishes , More trouble , But more in line with their own taste , And there's a lot of freedom .
contrast
- Stack is the function provided by the system , It's fast and efficient , The disadvantage is that there are limits , Data inflexibility ; Heap is a function provided by function library , It is characterized by flexibility and convenience , Data has a wide range of applications , But the efficiency has been reduced to a certain extent .
- Stack is the data structure of the system , For the process / Threads are unique ; Heap is the internal data structure of function library , Not necessarily the only one . Memory of different heap cannot operate with each other . Stack space is divided into static allocation and dynamic allocation . Static allocation is done by the compiler , Like automatic variables (auto) The distribution of . Dynamic allocation is made by
alloca
Function completion . Dynamic allocation of the stack does not require release ( It's automatic ), There is no release function . For the sake of portable programs , Dynamic allocation of stacks is not encouraged ! The allocation of heap space is always dynamic , Although all the data space will be released back to the system at the end of the program , But precise application memory / Free memory matching is good The basic elements of the program .
- Generally speaking, stack (
stack
) It often refers to the stack , First in, then out , It's a memory area . Local variables used to store programs , Temporary variable , The parameters of the function , Return address, etc . The allocation and release of variables in this area are automatically carried out by the system . No user involvement is required . And in the pile (heap
, fifo ) The space on is allocated by users , And the user is responsible for releasing .
边栏推荐
- Sqlserver encrypts and decrypts data
- 现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
- 复数在数论、几何中的用途 - 曹则贤
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- i. Mx6ull driver development | 24 - platform based driver model lights LED
- md5工具类
- Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- 力扣3_383. 赎金信
- POM in idea XML dependency cannot be imported
- 玩转gRPC—深入概念与原理
猜你喜欢
LOGO特训营 第二节 文字与图形的搭配关系
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
LOGO特训营 第五节 字体结构与设计常用技法
Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
PMO:比较25种分子优化方法的样本效率
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
LOGO特训营 第四节 字体设计的重要性
随机推荐
odps 中 对表进行了一次备份,为什么在元数据库查m_table 时,两张表的逻辑大小不一致,但数
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
Why is Dameng data called the "first share" of domestic databases?
WebGIS framework -- kalrry
赋能数字经济 福昕软件出席金砖国家可持续发展高层论坛
[advanced C language] array & pointer & array written test questions
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
Logo Camp d'entraînement section 3 techniques créatives initiales
AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
Jvm-Sandbox-Repeater的部署
LOGO特训营 第一节 鉴别Logo与Logo设计思路
力扣3_383. 赎金信
PostgreSQL JOIN实践及原理
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
Practice and principle of PostgreSQL join
智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
Solana链上应用Crema因黑客攻击停运