Note that the heap and stack areas referred to here refer to the (virtual) memory area, which is different from the stack in the data structure.
Stack area and heap area concepts
Stack--the compiler automatically allocates releases, stores the values of local variables, the information required to be maintained by function calls, and so on. The stack area must satisfy the rule that the stack is first entered and the stack is out, FILO.
Heap--typically released by programmers, if the programmer does not release, the program may end up being recycled by the operating system. In C + +, the heap area is generally applied by malloc/new.
The same thing is that they are all allocated in the operating system's virtual memory.
Stack and heap differences memory allocation and release
The stack area is automatically assigned by the compiler to release.
Heap areas are typically assigned and freed by programmers. If the programmer does not release, the program may end up being recycled by the operating system.
Address growth mode
The stack area is always growing to a low address. The operation of the stack to reduce the address of the top, the operation of the popup to increase the top address.
The heap area always grows to a high address.
Limit for size of application
The stack area is a contiguous area of memory, the size of which is pre-defined by the operating system (2M may be under Windows below). When the requested space exceeds the stack's remaining space, it will cause the stack to overflow and eventually cause the program to terminate. Therefore, the space available from the stack is smaller.
A heap area is not necessarily a contiguous area of memory (because the operating system uses a linked list to store free memory addresses), and its size is limited by the valid virtual memory in the operating system. Therefore, the space can be obtained from the stack more flexible, but also relatively large.
Response of the system after application
Stack area: As long as the remaining space of the stack is larger than the requested space, the operating system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
Heap Area: The operating system has a linked list that records the free memory address. When the system receives a request for a program, it iterates through the list, finds the heap node where the first space is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program. Also, for most systems, the size of this allocation is recorded at the first address in the memory space. This allows the Free/delete statement in the code to properly free up the requested memory space. Also, because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.
Comparison of application efficiency
The stack area is automatically assigned by the operating system and is faster. But programmers can't control it.
The heap area is allocated by the malloc/new memory, the general speed is relatively slow, and prone to memory fragmentation, but easy to use.
Problems that may be caused
When there is insufficient space left in the stack, it causes the stack to overflow.
Frequent application and release of heap areas can lead to memory fragmentation.
Resources
The difference between heaps and stacks (articles that have been turned over countless times)
What ' s the difference between a stack and a heap?
Stack area and heap area