Good article reprint: What is the difference between heap and stack?


Simple can be understood as: 
heap: the location of the space allocated by functions such as malloc. Addresses grow from low to high. 
stack: It is to automatically allocate variables and some space used when the function is called. The address decreases from high to low. 


Preliminary knowledge—program memory allocation 

A memory occupied by a program compiled by c/C++ is divided into the following parts 
1. Stack area (stack)—automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. . It operates like a stack in a data structure. 
2. Heap area (heap) - generally allocated and released by the programmer, if the programmer does not release it, it may be recovered by the OS at the end of the program. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list, haha. 
3. Global area (static area) (static)—The storage of global variables and static variables is put together, initialized global variables and static variables are in the same area, and uninitialized global variables and uninitialized static variables are in the same area. another adjacent area. - It will be released by the system after the program ends. 
4. Literal constant area—the constant string is placed here. Released by the system after the program ends. 
5. Program code area—stores the binary code of the function body. 

2. Example program 
This is written by a senior, very detailed 
//main.cpp 
int a = 0; global initialization area 
char *p1; global uninitialization area 
main() 

int b; stack 
char s[] = "abc" ; stack 
char *p2; stack 
char *p3 = "123456"; 123456 is in the constant area, p3 is on the stack. 
static int c = 0; global (static) initialization area 
p1 = (char *)malloc(10); 
p2 = (char *)malloc(20); 
the allocated area of ​​10 and 20 bytes is in the heap area. 
strcpy(p1, "123456"); 123456 is placed in the constant area, and the compiler may optimize it and the "123456" pointed to by p3 into one place. 



2. Theoretical knowledge of heap and stack 
2.1 Application method 
stack: 
automatically allocated by the system. For example, declare a local variable int b in the function; the system automatically opens up space for b in the stack 
heap: 
the programmer needs to apply for it himself, and specify the size, in the malloc function in c, 
such as p1 = (char *)malloc(10); 
Use the new operator in C++ 
such as p2 = (char *) malloc(10); 
but note that p1 and p2 themselves are on the stack. 
2.2 
System response 
stack after application: 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 stack overflow. 
Heap: First of all, you should know that the operating system has a linked list that records free memory addresses. When the system receives an application from a program, 
The linked list will be traversed to find the first heap node whose space is larger than the requested space, and then the node will be deleted from the free node linked list, and the space of the node will be allocated to the program. In addition, for most 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 heap node is not necessarily exactly equal to the requested size, the system will automatically put the redundant part back into the free list. 
2.3 Application Size Limitation 
Stack: Under Windows, the stack is a data structure that expands to low addresses, and is a continuous memory area. This sentence means that the address of the top of the stack and the maximum capacity of the stack are predetermined by the system. Under WINDOWS, the size of the stack is 2M (some say it is 1M, in short, it is a constant determined at compile time), if When the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, less space can be obtained from the stack. 
Heap: The heap is a data structure that expands to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low address to high address. The size of the heap is limited by the virtual memory available on the computer system. It can be seen that the space obtained by the heap is more flexible and larger. 
2.4 Comparison of application efficiency: 
the stack is automatically allocated by the system, which is faster. But the programmer is out of control. 
The heap is memory allocated by new, which is generally slow and prone to memory fragmentation, but it is the most convenient to use. In 
addition, under Windows, the best way is to use VirtualAlloc to allocate memory, which is not on the heap or on the stack It is to reserve a fast memory directly in the address space of the process, although it is the most inconvenient to use. But the speed is also the most flexible 
2.5 The storage content in the heap and stack 
Stack: When a function is called, the first thing that is pushed into the stack is the address of the next instruction after the main function (the next executable statement of the function call statement), and then the parameters of the function. In most C compilers , parameters are pushed onto the stack from right to left, and then local variables in the function. Note that static variables are not pushed onto the stack. 
When this function call ends, the local variables are popped out of the stack first, then the parameters, and finally the pointer on the top of the stack points to the first stored address, which is the next instruction in the main function, and the program continues to run from this point. 
Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific content in the heap is arranged by the programmer. 
2.6 Comparison of access efficiency 

char s1[] = "aaaaaaaaaaaaaaa"; 
char *s2 = "bbbbbbbbbbbbbbbbbb"; 
aaaaaaaaaaaa is assigned at runtime; 
bbbbbbbbbbb is determined at compile time; 
however, in subsequent access, Arrays on the stack are faster than pointers to strings (e.g. the heap). 
For example: 
#include 
void main() 

char a = 1; 
char c[] = "1234567890"; 
char *p = "1234567890"; 
a = c[1]; 
a = p[1]; 
return; 

corresponds Assembly code 
10: a = c[1]; 
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh] 
0040106A 88 4D FC mov byte ptr [ebp-4],cl 
11: 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 
The first type reads the elements in the string directly into the register cl when reading, The second method needs to point to edx first, and then read characters according to edx, which is obviously slow. 


2.7 Summary: 
The difference between the heap and the stack can be seen with the following metaphor: 
using the stack is like we go to a restaurant to eat, just order (issue an application), pay, and eat (use), and leave when you are full , You don’t have to pay attention to the preparation work such as cutting and washing vegetables, and the finishing work such as washing dishes and cleaning pots. Its advantage is that it is fast, but the degree of freedom is small. 
Using the heap is like cooking your favorite dishes by yourself. It is more troublesome, but it is more in line with your own taste and has a large degree of freedom. 

The difference between the heap and the stack is mainly divided into: 
the heap and the stack in the operating system, such as those mentioned above, not much to say. 
There is also the heap and stack in terms of data structure, these are different concepts. The heap here actually refers to a data structure of a priority queue (that satisfies the nature of the heap), and the first element has the highest priority; the stack is actually a mathematical or data structure that satisfies the nature of first-in-last-out. 
Although the stack and the stack are called together, there is still a big difference between them. The continuous call is only due to historical reasons.

Guess you like

Origin blog.csdn.net/fengqy1996/article/details/123902504