Document directory
- A metaphor for the difference between stack and stack
Let's look at a classic online example:
Main. cpp
Int A = 0; global initialization Zone
Char * P1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "ABC"; stack
Char * P2; stack
Char * P3 = "123456"; 123456 \ 0 is in the constant zone, and P3 is in the stack.
Static int C = 0; Global (static) initialization Zone
P1 = (char *) malloc (10); heap
P2 = (char *) malloc (20); heap
}
0. Different application methods and recovery methods
I don't know if you understand it. The first difference between stack and stack is that the application method is different: Stack (the English name is stack) is automatically allocated by the system. For example, we define a char; the system automatically opens up space for the stack. Heap (HEAP) is the space that programmers apply for as needed, such as malloc (10). It opens up ten bytes of space. Because the space on the stack is automatically allocated and automatically recycled, the life cycle of the data on the stack is only in the process of running the function. After running the function, the data is released and cannot be accessed. The data on the stack can be accessed as long as the programmer does not release space, but the disadvantage is that memory leakage will occur once the programmer forgets to release the data. There are other differences. I think my online friends have a good summary and I will repeat it here:
1. system response after application
Stack: As long as the remaining stack space is larger than the requested space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records the idle memory address. When the system receives a program application, it will traverse the linked list to find the heap with the first space greater than the requested space.
Node, delete the node from the idle node linked list, and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
That is to sayThe heap will do some subsequent work after the application, which will lead to the application efficiency problem.
2. Comparison of Application Efficiency
We can see from and.
Stack: Automatically assigned by the system, which is faster. But programmers cannot control it.
Heap: It is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
3. Application size limit
Stack: In Windows, the stack is a data structure extended to the low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
Heap: The heap is a data structure extended to the high address, and is a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
4. Storage content in heap and stack
Due to the limited size of the stack, It is physical to use subfunctions, not just logical.
Stack: When calling a function, the first entry to the stack isNext instruction after the function is called in the main function(The next executable statement of a function call statement), followed by the parameters of the function. In most C compilers, the parameters are pushed from right to left, and thenLocal variables in the function. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.
You can also refer to this question for storage content. This question also involves the survival of local variables.
5. Comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime and placed in the stack.
Bbbbbbbbbbbbb is determined during compilation and placed in the heap.
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:
# Include
Void main ()
{
Char A = 1;
Char C [] = "1234567890 ";
Char * P = "1234567890 ";
A = C [1];
A = P [1];
Return;
}
Corresponding 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
A metaphor for the difference between stack and stack
The difference between stack and stack can be seen from the metaphor of a senior:
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage is fast, but his degree of freedom is small.
Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom. The metaphor is very vivid and easy to understand. I don't know if you have any gains.