From the Internet, I saw it very detailed, I didn't change much :)
Slightly changed, and added some content, I hope the logic is clearer.
If you encounter multiple function calls in the future, add it to the back, huh, huh.
Transfer from: http://biwier.yculblog.com/post.484282.html
C / C memory area allocation
Nome @ 2005-01-20 17:36
1. Divided into these storage area in C
1. Stack - with the compiler automatic allocation release
2. Heap - Generally distributed by programmers, if the programmer does not release, the program may be recycled by OS
3. Global zone (static area), storage of global variables and static variables is placed on a piece of, initialized global variables and static variables in a region, uninited global variables and uninited static variables in adjacent> A zone. - Procedure end release
4. There is also a specialized place. - Procedure end release
The variables defined in the function body are typically on the stack, and the function assigned by malloc, calloc, realloc, etc. is on the heap. In all functions, in vitro is a full level, the Static modifier is added to the global zone (static zone), and the Static variable defined in all functions in vitro is indicated in this file, which cannot be valid in the file. For use, the Static defined in the function indicates that only in this function is valid. In addition, "ADGFDF" in the function is placed in a constant area. such as:
Code:
INT A = 0; // Global Initialization Area
Char * p1; // Globally uninited area
Main ()
{
INT B; / /
Char s [] = "abc"; // stack
Char * p2; // stack
Char * p3 = "123456"; //123456 {Post.content} In the constant zone, P3 is on the stack.
Static int C = 0; // Global (static) initialization area
P1 = (char *) malloc (10);
P2 = (char *) malloc (20);
// Allocate the area of 10 and 20 bytes in the pile area.
STRCPY (P1, "123456");
//123456} placed in a constant zone, the compiler may optimize "123456" pointed to by P3 into one.
}
II. In C , memory is divided into 5 districts, they are piles, stacks, free storage, global / static storage and constant storage areas.
1. Stack, that is, the storage area of the variable that is automatically cleared by the compiler when needed. The variables inside are usually local variables, function parameters, and the like.
2. Heap is those memory blocks allocated by new, their release compiler does not depends, controlled by our application, usually a New is to correspond to a delete. If the programmer is not released, the operating system is automatically recycled after the sequence is completed.
3. The free storage area is those memory blocks allocated by Malloc, and he and the heap are very similar, but it is used to end your own life.
4. Global / static storage, global variables, and static variables are assigned to the same memory, in the previous C language, global variables are divided into initialization and unexpected, and there is no difference in C , they together Occupy the same memory area.
5. Constant storage area, this is a relatively special storage area, where they are constant, are not allowed to modify (of course, you have to modify the non-normal means)
Now talk about the relationship and difference of the stack: Specifically, the modern computer (serial execution mechanism) is directly supported by the stack in the code underlying. This is reflected in that there is a special register pointing to the address where the stack is located, there is a special machine instruction to complete the data into the stack out> stack operation. This mechanism is characterized by high efficiency, limited data, generally integers, pointers, floating point numbers, etc., and other data types directly supported, and other data structures are not directly supported. Because the stack of this special> point, the use of the stack is very frequent in the program. The call to the subroutine is done directly by the stack. The machine's Call instruction implies the operation of pushing the return address into the stack, then jumps to the subroutine address, and the RET instruction in the subroutine is implicitly popped up from the stack and the operation of jump. Automatic variables in C / C are examples of direct use stacks, which is why the function of the function is automatically intented when the function returns.
Unlike the stack, the data structure of the heap is not supported by the system (whether it is a machine system or operating system), but is provided by the function library. The basic Malloc / Realloc / Free function maintains a set of internal stack data> structures. When the program uses these functions to get a new memory space, this set of functions first try to find available memory spaces from the internal stack. If there is no memory space available, try to utilize the system calling movement> state add program data segment The memory size, the newly assigned space first is first organized into the internal heap, and then returns to the caller in an appropriate form. When the program releases the assigned memory space, this memory space is> Returns the internal heap structure, which may be properly processed (such as incorporating other idle spaces into larger idle space), in order to make the next memory allocation application . This complex allocation mechanism is actually equivalent to a buffer pool (cache), which uses this mechanism.
1. System calls may not support any size memory allocation. Some systematic system calls only support fixed size and their multiple memory requests (by page assignment); this kind of waste will cause waste to a large number of small memory classifications.
2. System call requesting memory may be expensive. System calls may involve transformation of user-state and core states.
3. Memory allocation without management is easy to cause memory fragmentation under the distribution of complex memory.
Stacks and stacks:
From the above knowledge, the stack is the functionality provided by the system. It is characterized by rapid and efficient. The disadvantages are limited. The data is not flexible; and the stack is the function provided by the function library. It is flexible and convenient. The data adaptation is wide, but efficiency> reduce. The stack is the system data structure, and the process / thread is unique; the stack is the internal data structure in the library, not necessarily unique. The memory allocated by different stacks cannot operate with each other. Stack space is static allocation and dynamic allocation. Static allocation is the completion of the compiler, such as auto variables (AUTO). Dynamic allocation is done by the alloca function. The dynamic assignment of the stack does not need to be released (it is automatically), and there is no release function. For the procedure for portable>, the dynamic allocation operation of the stack is not encouraged! The allocation of the stack is always dynamic. Although all data spaces will be released back to the system at the end of the program, exact application memory / release memory> is equipped with a good program.
1. Debris problem: For the heap, frequent New / Delete will inevitably cause the memory space to discontinuous, resulting in a large amount of debris, making the program efficiency decrease. For the stack, there will be no such problem, because the stack is the first queue, they are such a one, so that there is no possibility of popping up from the middle of the stack, before he pops up, He has already popped up, detailed> Reference data structure, here we will not discuss one by one.
2. Growth direction: For heaps, the growth direction is up, that is, the direction increasing to the memory address; for the stack, its growth direction is down, and is a direction in which the memory address is reduced. 3. Allocation: Heap is dynamically allocated, no static allocation stacks. There are two types of allocation: static allocation and dynamic allocation. Static allocation is the completion of the compiler, such as partial variables. Dynamic assignments are allocated by the alloca function, but the dynamic allocation and stack of the stack is different. His dynamic allocation is released by the compiler, no need to implement it.
4. Assign efficiency: The stack is the data structure provided by the machine system. The computer provides support in the underlying pair of stacks: allocated the address of the stack, the stack out of the stack has a special instruction, which determines the efficiency of the stack Relay. The stack is provided by the C / C function library. Its mechanism is very complicated, for example, in order to assign a memory, the library function will search in the heap> memory in accordance with certain algorithms (specific algorithms) (specific algorithms) Available enough space, if there is not enough space (possibly due to too much memory debris), it is possible to call the system function to add the memory space of the program data segment, so there is a chance> Different enough memory, Then return. Obviously, the efficiency of the heap is much lower than the stack.
Clearly distinguished from points and stacks:
In BBS, the problem of stack and stack seems to be an eternal topic, which shows that beginners are often confused, so I decided to take him the first open knife.
First, let's give an example:
Void f () {INT * P = new int [5];
This short sentence contains a stack and stack, see New, let's first think that we allocate a stack of memory, then pointer P? He assigned a stack memory, so this sentence means that a pointer P is stored in a stack of memory in the stack memory. In the program, you will first determine the size of the memory in the stack, then call the Operator New allocated memory, then return the first address of this memory, put in the stack, the assembly code under VC6 is as follows:
00401028 Push 14h
0040102A Call Operator New (00401060)
0040102F Add ESP, 4
00401032 MOV DWORD PTR [EBP-8], EAX
00401035 MOV EAX, DWORD PTR [EBP-8]
00401038 MOV DWORD PTR [EBP-4], EAX
Here, how do we release it in order to simply release memory? Is DELETE P? Australia, wrong, should be delete [] p, this is to tell the compiler: I delete a array, VC6> will make the release of memory according to the corresponding cookie information.
Ok, we go back to our theme: What is the difference between the stack and stack?
The main difference is from the following points:
1. Different management methods;
2, the space size is different;
3. Can you generate a fragmentation;
4, the growth direction is different;
5, the allocation method is different;
6, the distribution efficiency is different;
Management method: For the stack, it is automatically managed by the compiler. No need to control it; for the heap, the release work is controlled by programmers, easy to generate Memory Leak. Space size: Under the 32-bit system, the heap memory can reach 4G space, from this perspective, the stack memory is almost no restrictions. However, for the stack, there is generally a certain space size, for example, under VC6, the default stack space size is 1M (like, not clear). Of course, we can modify:
Open the project, follow the operations the menu as follows: Project-> setting-> link, select Output in Category, and set the maximum value of the stack and commit in RESERVE.
Note: The RESERVE minimum is 4Byte; commit is reserved inside the page file of the virtual memory, which makes the stack to open a larger value, which may increase the overhead and startup time of the memory.
Compared to a large number of new / delete, it is easy to cause a large number of memory fragments; due to no special system support, the efficiency is very low; due to possible to trigger the user and the core state switch, the application, the cost of the memory becomes More expensive. So the stack is the most widely used in the program. Even if the call is called, the parameters, return addresses, EBP, and local variables are stored in the function call. So, we recommend everyone to use the stack, not the heap.
Also comparison with access efficiency:
Code:
Char S1 [] = "Aaaaaaaaaaaaaa";
Char * s2 = "bbbbbbbbbbbbbbb";
Aaaaaaaaaa is assigned at runtime;
Bbbbbbbbbb is determined when compiling;
However, in the later access, the array on the stack is faster than the string (e.g.,) pointed to by the pointer.
such as:
#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
The first specifically reads the elements in the string to the register cl while reading, and the second, first read the pointer value to the EDX, which is obviously slower in accordance with the EDX read characters.
Whether it is a stack or stack, it is necessary to prevent the occurrence of cross-bounds (unless you deliberately make it off), because the results of the offshore are either the procedure crash, or destroy the plot, stack structure, resulting in the result, even During your program running, there is no above problem, you still have to be careful, maybe when you collapse, write a stable and secure code is the most important :)