I. program memory allocation
The memory occupied by a C/C ++ compiled program is divided into the following parts:
1. Stack)
The compiler automatically assigns release, stores function parameter values, and local variable values. The operation method is similar to the stack in the data structure.
2. Heap)
Generally, it is assigned to the programmer for release. If the programmer does not release the program, it may be recycled by the OS at the end of the program. The allocation method is similar to the linked list.
3. Global (static )-,
The initialized global variables and static variables are in the same region. uninitialized global variables and uninitialized static variables are in the adjacent region BSS. The program is released by the system.
4. Text Constant Area
Constant strings are placed here. The program is released by the System
5. Code Area
Stores the binary code of the function body.
Ii. Example Program
// Main. CPP int A = 0; global initialization zone char * P1; Global uninitialized zone BSS 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); P2 = (char *) malloc (20 ); the allocated 10-byte and 20-byte areas are in the heap area. Strcpy (P1, "123456"); 123456 \ 0 is placed in the constant area, and the compiler may optimize it into a place with the "123456" that P3 points. }
Iii. Summary of differences between stack and stack
2.1 Application Method
STACK:
Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up null for B in the stack.
Room
Heap:
The programmer needs to apply and specify the size. In C, the malloc Function
For example, P1 = (char *) malloc (10 );
Use the new operator in C ++
For example, P2 = new char [10];
But note that P1 and P2 are in the stack.
2.2 system response after application
STACK: as long as the remaining stack space is larger than the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Output.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
The linked list is traversed to find the heap node with the first space greater than the requested space, and then the node is linked from the idle node list.
And allocate the space of the node to the program. In addition, for most systems
The size of the allocation is recorded at the first address. In this way, 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 will automatically
Into the idle linked list.
2.3 Application size limit
STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. MEANING OF THIS SENTENCE
Thinking is that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (there are also
1 m, which is a constant determined during compilation). If the requested space exceeds the remaining space of the stack
The prompt is "overflow. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses linked lists for storage.
The idle memory address is not consecutive, And the traversal direction of the linked list is from the low address to the high address. Heap size
Limited by valid virtual memory in computer systems. It can be seen that the space obtained by the heap is flexible and large.
2.4 comparison of application efficiency:
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
In addition, in windows, the best way is to use virtualalloc to allocate memory, which is neither heap nor stack.
A piece of memory is directly reserved in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.
Storage content in 2.5 heap and stack
STACK: when calling a function, the first entry to the stack is the next instruction of the main function (the next instruction of the function call statement can be
Statement execution), and then the parameters of the function. In most C compilers, the parameters are written from right to left into the stack.
And then the local 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 last stack points to the location where it was originally stored.
Address, 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.
2.6 comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during compilation;
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
The first type reads the elements in the string directly into the CL register, while the second type reads the pointer value first.
Reading characters based on edX is obviously slow.