I. Stack and stack in the Data Structure
1Stack
An advanced and post-output data structure. This data structure is often used for analysis in recursive calls.
2, Heap
Heap is a Sort tree data structure with each node having a value. Generally, the data structure of a heap refers to a binary heap. Heap is characterized by the smallest (or largest) value of the root node, and the two subtree of the root node is also a heap. Because of this feature of heap, it is often used to implement priority queues, and the access to heap is random. This is like reading books on the bookshelves of the library. Although the books are placed in order, but when we want to retrieve any book, we don't have to take out all the books in front of the stack. Unlike the box, we can retrieve the books we want directly.
Ii. Memory heap and stack
1 first understand C/C ++ProgramMemory usage:
1.1,Stack area (Stack)
The compiler automatically assigns release, stores function parameter values, and local variable values. Operator
Similar to the stack in the data structure.
1.2,Heap area (Heap)
Generally, it is assigned to the programmer for release. If the programmer does not release the program, the program may endOSRecycle. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
1.3,Global zone (static Zone )(Static)
Global variables and static variables are stored in one area, and initialized global variables and static variables are stored in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system.
1.4,Text Constant Area
Constant strings are placed here. The program is released by the System
1.5,ProgramCodeZone
Stores the binary code of the function body.
2. Differences
2.1 application method
Stack) automatically assigned by the system. For example, declare a local variable in the function int B; the system automatically sets B open up space .
the heap must be applied by the programmer and the size is specified. in C medium Use malloc function , for example, P1 = (char *) malloc (10 ); in C ++ use New operator , for example, char * P2 = new (char *) (10);
However, it should be noted that the pointer variable itself is in the stack, but the memory space allocated to it is in the heap.
Above P1 , P2 .
2.2 System Response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
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, it will traverse the linked list, find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space. Delete Statement to correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
2.3 Application size limit
STACK: In Windows Lower , Stack is a data structure extended to a low address and a continuous memory area. This statement means that the top stack address and the maximum stack capacity are pre-defined by the system. Windows The stack size is 2 m (Some also say 1 m ) If the requested space exceeds the remaining space of the stack, a prompt will be displayed. 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 the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. 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.
Heap New The allocated memory is generally slow and prone to memory fragments. , However, it is most convenient to use. .
In addition Windows The best way is to use Virtualalloc Allocate memory. It is not in the heap, nor in the stack. It is directly stored in the address space of the process, although it is the most inconvenient to use. But the speed is also the most flexible
2.5 Storage content in heap and stack
STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C In the compiler, parameters are written from right to left into the stack, followed by 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 stack points to the address of the initial storage, 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 Values are assigned at runtime;
While Bbbbbbbbbbbbb It is determined during compilation;
However, in future access, the array on the stack is better than the string pointed to by the pointer. ( For example, heap ) Fast.
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
First, read the elements in the string directly to the Register. Cl And the second type must first specify EdX In EdX Reading characters is obviously slow.