当前位置:网站首页>C language | the difference between heap and stack
C language | the difference between heap and stack
2022-06-26 14:09:00 【wekidi】
- Management style : Automatic stack management system ; The heap is controlled by the programmer , Easy to use , But it is prone to memory leakage .
- Direction of growth : Stack extends to low address ( namely ” Downward growth ”), It's a continuous memory area ; The heap extends to higher addresses ( namely ” Growing up ”), It's a discontinuous memory area . This is because the heap management system uses a linked list to store free memory addresses , Natural discontinuity , The linked list traverses from low address to high address .
- The size : The stack top address and the maximum capacity of the stack are predetermined by the system ( Usually the default is 1M or 10M); The size of the heap is limited by the virtual memory available in the computer system ,32 position Linux Heap memory in the system can reach 2.9G Space .
- Store content : Stack on function call , The first thing to push in is the function argument , Then the next instruction in the calling function ( The next executable statement of a function call statement ) Press your address into , Finally, the local variables of the called function . After this call , Local variables first out of stack , Instruction address out of stack , Finally, stack balance , The program continues to run the next executable statement from this point . The heap usually stores its size in a byte in the header , Heap is used to store data whose lifetime is independent of function call , The specific content is arranged by the programmer .
- Distribution efficiency : The stack is supported by the underlying computer : Allocate a special register to store the stack address , Pressing and exiting the stack are executed by special instructions , So it's more efficient . Heap is provided by function library , The mechanism is complex , Much less efficient than stack .
- Distribution method : Stacks can be allocated statically or dynamically . Static allocation is done by the compiler , Such as the distribution of local variables . Dynamic allocation is made by alloca Function to request space on the stack , Automatic release after use without transfer free function . The heap can only be allocated dynamically and released manually .
- System response after allocation : As long as the remaining stack space is larger than the requested space , The system will provide memory for the program , Otherwise, an exception prompt stack overflow is reported . The operating system maintains a linked list of free memory addresses for the heap . When the system receives the memory allocation request of the program , It will traverse the linked list to find the first heap node whose space is greater than the applied space , Then delete the node from the free node list , And the node space is allocated to the program . If there is not enough space ( Probably due to too many memory fragments ), It is possible to call system functions to increase the memory space of program data segments , In order to have a chance to allocate enough memory , Then go back . Most systems will record the memory size allocated this time at the first address of the memory space , For subsequent release functions ( Such as free/delete) Free the memory space correctly . Besides , Because the size of the heap node found may not be exactly equal to the size of the application , The system will automatically put the redundant part back into the free linked list .
- Debris problem : There will be no fragmentation of the stack , Because the stack is an in and out queue , Before the memory block is ejected from the stack , The contents of the backward stack above it have been ejected . However, frequent requests for free operation will cause the discontinuity of heap memory space , It's a lot of debris , To reduce the efficiency of a program .
so , Heap can easily cause memory fragmentation ; Because there is no special system support , Efficiency is very low ; Because it may cause user mode and kernel mode switching , Memory requests are more expensive . So stack is the most widely used in programs , Function calls also use the stack to complete , Parameters in the call process 、 The return address 、 Stack based pointers and local variables are stored in stack mode . therefore , It is recommended to use stack as much as possible , Use heap only when allocating large or large amounts of memory space .
边栏推荐
- 网络远程访问的方式使用树莓派
- 9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
- 9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
- d检查类型是指针
- 证券开户安全吗,有没有什么危险啊
- 虫子 类和对象 上
- Nexys A7开发板资源使用技巧
- [ahoi2005] route planning
- 同花顺股票开户选哪个证券公司是比较好,比较安全的
- 7.Consul服务注册与发现
猜你喜欢

爱可可AI前沿推介(6.26)

永远不要使用Redis过期监听实现定时任务!

Gartner 2022 Top Strategic Technology Trends Report

ThreadLocal giant pit! Memory leaks are just Pediatrics

Select tag - uses the default text as a placeholder prompt but is not considered a valid value

7.consul service registration and discovery

去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程

使用 Performance 看看浏览器在做什么

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

Range of types
随机推荐
[hnoi2010] flying sheep
Codeforces Round #765 (Div. 2) D. Binary Spiders
ThreadLocal巨坑!内存泄露只是小儿科...
Firewall introduction
永远不要使用Redis过期监听实现定时任务!
Exercises under insect STL string
9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
网络远程访问的方式使用树莓派
GC is not used in D
使用 Performance 看看浏览器在做什么
古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資
First k large XOR value problem
Range of types
Original code, inverse code and complement code
Mongodb series window environment deployment configuration
Self created notes (unique in the whole network, continuously updated)
Wechat applet SetData dynamic variable value sorting
[proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
Es common grammar I
Here document interaction free and expect automatic interaction