The difference between the stack and stack

xiaoxiao2021-03-19  210

I have been unclear the difference between the two, and I have been looking for their differences. This article found today made me removing the arms in my heart. Thanks to the author of the article. (The author is unknown, sorry.)

Key words stack

Summary

text

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 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) - Another area of ​​adjacent. - There is system release 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 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, wash, etc., etc., etc., brush the pot, 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. (classic!)

In BBS, the problem of stack and stack seems to be an eternal topic, which shows that beginners are often confused, so I decided to take him the first open knife. First, we will give an example: void f () {int * p = new int [5];} This short sentence contains the stack and stack, see New, we should first think of it, we assigned A pile of memory, then pointer P? He assigned a stack memory, so this sentence means that a pointer P is stored in a stack of memory in the stack memory. In the program, you will determine the size of the memory in the stack, then call the Operator New allocated memory, then return the first address of this memory, put in the stack, his assembly code under VC6 is as follows: 00401028 Push 14H 0040102A Call Operator New (00401060) 0040102032 MOV DWORD PTR [EBP-8], EAX 00401035 MOV EAX, DWORD PTR [EBP-8] 00401038 MOV DWORD PTR [EBP-4], EAX Here, we have not released memory So how do you release it? Is DELETE P? Australia, wrong, it should be delete [] P, this is to tell the compiler: I deleted an array, VC6 will work according to the corresponding cookie information to release the memory. Ok, we go back to our theme: What is the difference between the stack and stack? The main difference is different from the following points: 1. Different management methods; 2, the space size is different; 3, can generate fragmentation; 4, the growth direction is different; 5, the allocation mode is different; In the stack, it is automatically managed by the compiler. No need to control it; for the heap, the release work is controlled by programmers, which is easy to generate Memory Leak. Space size: Under the 32-bit system, the heap memory can reach 4G space, from this perspective, the stack memory is almost no restrictions. However, for the stack, there is generally a certain space size, for example, under VC6, the default stack space size is 1M (like, not clear). Of course, we can modify: Open the project, follow the following: Project-> setting-> link, select Output in Category, then set the maximum value of the stack and commit in RESERVE. Note: The RESERVE minimum is 4Byte; commit is reserved inside the page file of the virtual memory, which makes the stack to open a larger value, which may increase the overhead and startup time of the memory. Debris Problem: For heaps, frequent New / Delete will inevitably cause discontinuous memory space, resulting in a large amount of debris, making program efficiency decrease.

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

New Post(0)