First, prepare knowledge-program memory distribution
A program that is compiled by C / C is divided into the following parts.
1. Stacking area (STACK) - Automatically assigns the parameter value of the release, the parameter value of the function, and the like of local variables. its
The operation mode is similar to the stack in the data structure.
2, the heap - generally distributed by the programmer, if the programmer does not release, the program may be back when the program
Receipt. Note that it is two things with the stack in the data structure, and the allocation mode is similar to the linked list, huh, huh.
3, the global zone (static area) -, the storage of global variables and static variables is placed in one, initialized
Global variables and static variables in a region, uninited global variables and uninited static variables in adjacent
A zone. - Release by the system after the end of the program.
4, text constant zone - constant strings are placed here. Released by the system after the end
5, program code area - store the binary code of the correspondence.
Second, the example program
This is a predecessor written, very detailed
//main.cpp
INT A = 0; global initialization area
Char * p1; global uninited area
Main ()
{
INT B; Stack
Char s [] = "abc"; stack
Char * p2; stack
CHAR * P3 = "123456"; 123456/0 In the constant region, 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/0 placed in a constant zone, the compiler may put it with "123456" pointed to by P3
Optimize into one place.
}
Second, the theoretical knowledge of the stack and stack
2.1 application method
Stack:
Automatically assigned by the system. For example, declare a partial variable INT B in a function; system automatically opens up in the stack
between
HEAP:
Require programmers to apply, and specify the size, in the Malloc function
Such as p1 = (char *) malloc (10);
Use new operators in C
Such as p2 = new char [10];
But pay attention to P1, P2 itself is in the stack.
2.2
Response of the system after application
Stack: As long as the remaining space of the stack is greater than the application space, the system will provide the program to provide the program, otherwise the abnormality will be reported to the stack.
Out.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives the application,
Will traverse the list, look for the first space greater than the stack of the applied space, and then the node from the idle node linked list
Delete, and assign the space of the node to the program, in addition, for most systems, it will be in this memory space
The first address is recorded in this allocation, so that the delete statement in the code can release this memory space correctly.
In addition, since the size of the stacking point found is not necessarily equal to the size of the application, the system will automatically save the extra
Add it in the idle chain table.
2.3 Restrictions on the application size
Stack: Under Windows, the stack is a data structure extending to the low address, which is a contiguous memory area. This sentence is the meaning of this sentence
The maximum capacity of the address and stack of the stack is that the system is pre-stated in advance. Under Windows, the stack is 2m (also said that it is 1M, in summary is a constant determined when compiling), if the space is exceeded When the remaining space of the stack will
Tip Overflow. Therefore, the space available from the stack is smaller.
Heap: Heap is a data structure extended to a high address, which is a discontinuous memory area. This is because the system is stored in a linked list.
The idle memory address, nature is discontinuous, and the traversal direction of the linked list is from the low address to the high address. Pile size
Affected in a computer system effective virtual memory. It can be seen that the space obtained is flexible, it is relatively large.
2.4 Comparison of application efficiency:
The stack is automatically assigned by the system, the speed is faster. But the programmer is uncontrollable.
Stacks are allocated by NEW, generally slower, and it is easy to generate memory fragments, but it is most convenient to use.
In addition, under Windows, the best way is to allocate memory with Virtualalloc, he is not a heap, nor is it in the stack is
Keep a memory directly in the address space of the process, although it is the most inconvenient. But the speed is fast and flexible.
2.5 Storage content in the stacks and stacks
Stack: When the function is called, the first inrest is the next instruction in the main function (the next one of the function call statement)
Execute the address of the statement), then the various parameters of the function, in most C compilers, the parameters are from right left stacks
, Then the local variable in the function. Note that the static variable is not in the stack.
When this function call is completed, the local variable first puts the stack, then the parameters, the last stack of top pole points to the beginning of the beginning
The location is the next instruction in the main function, and the program continues to run by this point.
Pile: Generally, in the head of the heap, the size of the stack is placed in a heap. The specific content in the heap is arranged by the programmer.
2.6 Comparison of access efficiency
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 is to read the pointer value first.
In the EDX, it is obviously slower based on the EDX read characters. 2.7 Summary:
The difference between the stacks and stacks can be seen in the following metaphors:
The use stack is like we went to the restaurant to eat, just order dishes (send out), pay, and eat (use), eat
Go, don't pay attention to the cutting, wash, etc., and have a dishwashing, brush the pot and other sweeping work, his benefits are fast, but
The degree is small.
It is like a dish who is doing yourself, but it is more troublesome, but it is more troublesome.
Da Da.