The difference between the stack and stack (reproduced)

xiaoxiao2021-03-06  47

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, let's give an example:

Void f () {INT * P = new int [5];

This short sentence contains a stack and stack, see New, let's first think that we allocate a stack 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 first 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, the assembly code under VC6 is as follows:

00401028 Push 14h

0040102A Call Operator New (00401060)

0040102F Add ESP, 4

00401032 MOV DWORD PTR [EBP-8], EAX

00401035 MOV EAX, DWORD PTR [EBP-8]

00401038 MOV DWORD PTR [EBP-4], EAX

Here, how do we release it in order to simply release memory? 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 from the following points:

1. Different management methods;

2, the space size is different;

3. Can you generate a fragmentation;

4, the growth direction is different;

5, the allocation method is different;

6, the distribution efficiency is different;

Management method: For the stack, it is automatically managed by the compiler. No need to control it; for the heap, the release work is controlled by programmers, 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 operations the menu as follows: Project-> setting-> link, select Output in Category, and 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. For the stack, there will be no such problem, because the stack is the first queue, they are such a one, so that there is no possibility of popping up from the middle of the stack, before he pops up, He has already popped up, detailed can refer to the data structure, here we will not discuss one by one. Growth direction: For heaps, the growth direction is up, that is, the direction increasing to the memory address; for the stack, its growth direction is downward, which is to grow toward the direction of memory addresses.

Assignment: Heap is dynamically allocated, no static allocation stacks. There are two types of allocation: static allocation and dynamic allocation. Static allocation is the completion of the compiler, such as partial variables. Dynamic assignments are allocated by the alloca function, but the dynamic allocation and stack of the stack is different. His dynamic allocation is released by the compiler, no need to implement it.

Assigning Efficiency: The stack is the data structure provided by the machine system. The computer provides support in the underlying pair of stacks: allocated the address of the stack, the stack out of the stack has a special instruction, which determines the efficiency of the stack. . The stack is provided by the C / C function library. Its mechanism is very complicated, for example, in order to assign a single memory, the library function will search in the stack memory in the stack memory in accordance with certain algorithms (specific algorithms). Sufficient size space, if there is no size enough space (may be too much memory fragmentation), there is a possibility to call the system function to add the memory space of the program data segment, so there is a chance to share the size of the memory, then return. Obviously, the efficiency of the heap is much lower than the stack.

From here we can see, due to the use of a large number of new / delete, it is easy to cause a large amount of memory fragments; due to the unmanaged system support, the efficiency is very low; due to possible switching, The application of memory is more expensive. So the stack is the most widely used in the program. Even if the call is called, the parameters, return addresses, EBP, and local variables are stored in the function call. So, we recommend everyone to use the stack, not the heap.

Although there are so many benefits, it is not so flexible compared with the heap, sometimes allocates a lot of memory space, or uses a stack of stacks.

Whether it is a stack or stack, it is necessary to prevent the occurrence of cross-bounds (unless you deliberately make it off), because the results of the offshore are either the procedure crash, or destroy the plot, stack structure, resulting in the result, even During your program, there is no problem, you still have to be careful, maybe when you can't collapse, then Debug is quite difficult :)

By the way, there is one thing, if someone puts up the stack, then it means that the stack is not a pile, huh, oh, clear?

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

New Post(0)