The difference between the stack and stack

xiaoxiao2021-04-08  295

The difference between the stack and stack

First, prepare knowledge? Memory allocation of programs

A program that is compiled by C / C is divided into the following parts.

1. Stack Area (STACK) Automatically assigns the parameter value of the release, the parameter value of the function, and the like of local variables. Its operation is similar to the stack in the data structure.

2, the heap? Generally, the programmer is allocated, 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 zone) (static)? The storage of global variables and static variables is placed in a piece of, initialized global variables and static variables in a region, uninited global variables and uninited static variables in phase Another area of ​​adjacent. - There is system release after the end of the program

4, text constant area? The constant string is placed here. Released by the system after the end

5. The program code area? Stores 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 optimize "123456" pointed to by P3 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 local variable INT B in a function, automatically opens up space in B.

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 = (char *) malloc (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 applied space, the system will provide the program to provide 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,

Will traverse the list, look for the first space greater than the stack of the applied space, then remove the node from the idle node lin list, and assign the space of the node to the program, and for most systems, The size of this assignment is recorded at the first address in this memory space, so that the delete statement in the code can release the 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 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 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.

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 in a heap, nor is it in the stack to keep a fast 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 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 In the parameter, the parameter is from right 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 [] = "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.

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, only disserted (request), pay, and eat (use), eat, don't pay attention to the food, washing, etc., etc. Sweeping, his benefits are fast, but the freedom is small. It is like it is a dish who is doing it yourself, but it is more troublesome, but it is more troublesome.

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

New Post(0)