The following content comes from the study and arrangement of network resources. If there is any infringement, please inform and delete it.
1. How to apply
(1) stack
automatically assigned by the system. For example, if a local variable "int b;" is declared in the function, the system will automatically open up space for b in the stack.
(2) heap
The programmer needs to apply by himself and specify the size.
Use the malloc function in C to apply, such as " p1=(char*)malloc(10); ".
Use the new operator in C++, such as "p2 = new char[10]; ".
Note that p1 and p2 themselves are on the stack.
2. System response after application
(1) stack
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.
(2) heap
The operating system has a linked list that records free memory addresses. 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 remove the node from the free node linked list. Delete, and allocate the space of this node to the program. Most systems will record the size of this allocation at the first address in this memory space, so 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.
3. Application size limit
(1) stack
Under Windows, the stack is a data structure that extends to low addresses and is a continuous area of memory. 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 the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, less space can be obtained from the stack.
(2) 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, so it is 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.
4. Comparison of application efficiency
(1) stack
The stack is automatically allocated by the system, which is faster. But the programmer is out of control.
(2) heap
The heap is memory allocated by using malloc related functions or 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 directly reserves a piece of memory in the address space of the process. Although it is the most inconvenient to use, it is fast and flexible.
5. Storage content in the heap and stack
(1) stack
When a function is called, the first thing that is pushed onto the stack is the address of 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) , followed by 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.
(2) 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.
6. Comparison of access efficiency
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
In the above code, aaaaaaaaaaa is assigned at runtime, while bbbbbbbbbbb is determined at compile time. On subsequent accesses, an array on the stack is faster than a string pointed to by a pointer (such as the heap).
for example:
#include<stdio.h>
int main(int argc,char* argv[])
{
char a=1;
char c[] = "1234567890";
char *p = "1234567890";
a = c[1];
a = p[1];
return;
}
The 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
The first type directly reads the elements in the string into the register cl when reading, while the second type needs to read the pointer value into edx first, and then read the characters according to edx, which is obviously slow.
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 (send an application), pay and eat (use), and leave when you are full, regardless of the preparation work such as cutting and washing vegetables, as well as washing dishes and washing pots Wait for the finishing work. Its advantage is fast, but the degree of freedom is small.
Using heaps 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 lot of freedom.