Stack area (stack): Automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. Its operation is similar to a stack in a data structure.
Heap area (heap): Generally allocated and released by the programmer, if the programmer does not release, it may be recycled by the operating system at the end of the program. It is different from the heap in the data structure, and the allocation method is similar to a linked list.
The difference
How to apply
Stack: automatically allocated by the system. For example, when declaring a local variable b := 0 of a function, the system will automatically open up space for b in the stack.
Heap: The programmer needs to apply for it and specify the size. In C, use the malloc function, in C++ use the new operator, and in Go language, use escape analysis to determine which variables should be allocated on the stack and which variables should be on the heap Allocations, including memory allocated implicitly using methods such as new, make, and literals. Escape analysis in Go follows the following two invariants:
- Pointers to stack objects cannot exist on the heap;
- Pointers to stack objects cannot survive stack object recycling;
Response to the system after application
Stack: As long as the remaining space of the stack is greater than the requested space, the system will provide memory for the program, otherwise an exception will be reported indicating stack overflow.
Heap: The operating system has a linked list that records the memory address of the space. When the system receives an application from a program, it will traverse the linked list to find the first heap node whose space is larger than the requested space, and then delete the node from the linked list of free memory nodes. And allocate the space of this node to the program. For most operating systems, the size of this allocation will be recorded at the first address in this memory space, so that the delete statement in the code can correctly release this memory space. In addition, since the size of the found node is not necessarily exactly equal to the size of the application, the system will automatically put the redundant part back into the linked list.
Application size limit
Stack: Under Windows, the stack is a data structure that expands to low addresses, and is a continuous memory area. The fixed address and the size of the stack are predetermined by the system, if the requested memory space exceeds the remaining space of the stack. Will prompt stack overflow.
Heap: The heap is a memory structure that expands to high addresses and is a discontinuous memory area. It is a discontinuous space where the system uses a linked list to store free memory addresses.
Comparison of Application Efficiency
Stack: Automatically allocated by the system, the speed is faster, but the programmer cannot control it.
Heap: The memory allocated by the program is generally slow and prone to memory fragmentation, but it is convenient to use.
Storage contents of the heap and stack
Stack: When a function is called, the first thing pushed into the stack is the address of the next instruction of the main function (the next executable statement of the function call), and then the parameters of the function. In a C compiler, parameters are pushed onto the stack from right to left, followed by function local variables. Static variables are not pushed onto the stack.
Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific content of the heap is arranged by the programmer. In terms of data structure, the heap here refers to a data structure of the priority queue, and the first element has the highest priority.
summary
The stack space is automatically allocated and released by the operating system, and the space on the heap is allocated and released by the program.
The stack space is limited, and the heap is a large free memory area.
Different languages have different ways of creating the heap. The malloc function is used in C, the new operator is used in C++, and in Go language, escape analysis is used to determine which variables should be allocated on the stack and which variables should be allocated on the heap. , which includes implicit allocation of memory using methods such as new, make, and literals.