Take a look at a classic example that is popular online:
main.cpp int a = 0; Global initialization zone char *P1; The global uninitialized area main () {int b; stack char s[] = "abc"; stack char *p2; stack char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack. static int c = 0; global (static) initialization zone P1 = (char *) malloc (n); Heap P2 = (char *) malloc (a); Heap
0. Different application methods and recycling methods
I don't know if you understand that the first difference between heaps and stacks is that the application is different: the stack (English name is stack) is the system automatically allocated space, for example we define a char A; the system automatically opens up space on the stack. The heap (the English name is heap) is the space that the programmer applies for, such as malloc (10), and 10 bytes of space to open. Since the space on the stack is automatically collected automatically, the life cycle of the data on the stack is only run in the function, it is released after running, and can no longer be accessed. The data on the heap can be accessed as long as the programmer does not free up space, but the drawback is that once you forget to release it will cause a memory leak. There are some other differences I think the online friends summed up well here to report: 1. Post-application response stack: As long as the remaining space on the stack is larger than the requested space, the system will provide memory for the program, otherwise the exception prompt stack overflow. Heap: The first thing you should know is that the operating system has a linked list that records the free memory address, and when the system receives the application, it iterates through the list, looking for the first heap that is larger than the requested space. node, then removes the node from the list of idle nodes and assigns the node's space to the program, and for most systems, the size of this allocation is recorded at the first address in the memory space, so that the DELETE statement in the code can properly free up the 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. That is, the heap will have to do some follow-up work after the application, which will lead to the question of application efficiency. 2. The comparison of application efficiency is based on No. 0点 and 1th. Stack: Automatically allocated by the system, faster. But programmers can't control it. Heap: Is the memory allocated by new, the general speed is slow, and prone to memory fragmentation, but the most convenient to use. 3. Application size limit stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small. Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large. 4. Storage content in heaps and stacks due to the large stackSmall, so the use of sub-functions or physical meaning, and not just logical meaning. Stack: When the function is called, the first stack is the address of the next instruction (the next executable statement of the function call statement) after the function call in the main function, and then the parameters of the function, in most C compilers, the arguments are in the right-to-left stack, followed by the local variables in the function. Note that static variables are not in the stack. At the end of this function call, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point. Heap: The size of the heap is usually stored in the heap with a byte in the head. The concrete contents of the heap are arranged by programmers. You can also refer to this question for storage content. This problem also involves the survival period of local variables. 5. Comparison of access efficiency char s1[] = "AAAAAAAAAAAAAAA"; char *s2 = "BBBBBBBBBBBBBBBBB"; aaaaaaaaaaa is assigned at run time; put in the stack. BBBBBBBBBBB is determined at compile time; it is placed in the heap. However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap). such as:
The corresponding assembly code
10:a = c[1]; 00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh] 0040106A 4D FC mov byte ptr [ebp-4],cl 11:a = p[1]; 0040106D 8B-EC mov edx,dword ptr [ebp-14h] 00401070 8A mov al,byte ptr [edx+1] 00401073 FC mov byte ptr [eb P-4],alSummaryThe difference between heap and stack can be quoted by a predecessor analogy to see: the use of stacks like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but small The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom.
Getting started with the C language stack-the difference between heaps and stacks