The difference between the stack and stack

xiaoxiao2021-03-06  61

The difference between the stack and stack, the preparatory knowledge-program memory allocation of a program occupied by C / C compiled program is divided into the following sections 1, the stack area (STACK) - Automatically assigns the release, store the parameters of the function Value, local variable value, etc. Its operation is similar to the stack in the data structure. 2, the heap - generally distributed by programmers, if the programmer does not release, the program may be recycled by OS. 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, global zone (static area) -, global variables and static variables are stored in one, initialized global variables and static variables in a region, uninited global variables and uninited static variables in phase Another area of ​​adjacent. - After the program is completed, there is system release 4, the text constant area - constant strings are placed here. After the program is completed, the system is released 5, the program code area - the binary code of the function body. Second, the example program is written by a predecessor, very detailed //main.cppint a = 0; global initialization zone char * p1; global unin-initialization zone main () {INT B; stack char s [] = "abc"; Stack Char * P2; Stack Char * P3 = "123456"; 123456/0 In the constant zone, P3 is on the stack. Static int C = 0; global (static) initialization zone p1 = (char *) malloc (10); p2 = (char *) malloc (20); allocated 10 and 20 bytes of areas are in the pile area. STRCPY (P1, "123456"); 123456/0 placed in a constant zone, the compiler may optimize "123456" pointed to by P3 into one place. } II, the theoretical knowledge of the stack and stack 2.1 Application Stack: Automatic allocation by the system. For example, declaring a local variable INT b in a function, the system automatically opens up space in the stack: It is required to apply for the programmer, and specify the size, Malloc function in C, such as p1 = (char *) malloc (10); In C , use new operators such as P2 = (char *) malloc (10); but pay attention to P1, P2 itself is in the stack. 2.2 Response stack of the system after the application: As long as the remaining space of the stack is greater than the application space, the system will provide memory for the program, otherwise the abnormality prompts are overflow. Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives the application, it will traverse the list, find the first space greater than the stack of the applied space, then the node Delete in the idle node linked list, and assign the space of the node to the program, in addition, for most systems, the size of this allocation is recorded in the first address in this memory space, so that the DELETE statement in the code. Can release this memory space correctly. In addition, since the size of the stack of stacks is not necessarily equal to the size of the application, the system will automatically re-put the excess part in the idle chain table. 2.3 Restriction Stack of Application Size: Under Windows, the stack is the data structure extending to the low address, which is a contiguous memory area.

This sentence means that the maximum capacity of the address and stack of the stack is that the system is pre-specified. Under Windows, the stack size is 2m (more saying is 1M, in summary is a constant when compiling), if When the application's space exceeds the remaining space of the stack, Overflow will be prompted. 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 the idle memory address stored by the linked list, and it is naturally discontinuous, and the traversal direction of the linked list is from the low address to the high address. The size of the heap is limited to the effective virtual memory in the computer system. 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. The stack is a memory allocated by New, and it is easy to generate memory debris, but it is most convenient. In addition, under Windows, the best way is to allocate memory with Virtualalloc, he is not a pile, not in the stack It is directly in the address space of the process to keep a fast memory, although it is most inconvenient to use. But the speed is fast and flexible. 2.5 Storage Content Stack in Heap and Stack: When the function is called, the first inrest is the address of the next instruction after the main function (the next executable statement of the function call statement), and then the various parameters of the function In most C compilers, the parameters are from right to left, and then partial variables 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 top pointer points to the starting address, which 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 pile has programmers. 2.6 Comparison of access efficiency Char S1 [] = "aaaaaaaaaaaaaaa"; char * s2 = "bbbbbbbbbbb"; Aaaaaaaaaaa is assigned at runtime; and bbbbbbbbbb is determined when compiling; however, in future access, The array on the stack is fast than the string (e.g., the pile) pointed to by the pointer. 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 Only the element in the string is directly in the register CL, and the second is to read the pointer value to the EDX, which is obviously slower in accordance with the EDX read characters.

转载请注明原文地址:https://www.9cbs.com/read-111031.html

New Post(0)