"Yoga Mountain Night" ---- Memory Allocation (1)

zhaozj2021-02-16  50

Summary: Memory management is always a mine area for C / C programming. Everyone is not very willing to touch her, but sometimes have to touch it. While using Smart Pointer in C It is already possible to completely avoid using pointers, it helps us to write more efficient code for further understanding of pointers, which helps us understand programs previously prepared.

The five major memory partitions are divided into 5 districts in C , which are piles, stacks, free storage, global / static storage and constant storage. Stack is the storage area that is automatically clear when it is assigned by the compiler when needed, and automatically clear the storage area of ​​the variable when not needed. The variables inside are usually local variables, function parameters, and the like. Heap is those memory blocks allocated by new, their release compiler does not depends, and is controlled by our application. Generally, a New is to correspond to a delete. If the programmer is not released, the operating system will automatically reclaim after the program is over. The free storage area is those that are allocated by Malloc, and he and the heap are very similar, but it uses free to end your own life. The global / static storage area, global variables and static variables are assigned to the same memory. In the previous C language, the global variable is divided into initialization and unexpected, and there is no difference in C , they share the same A memory area. A constant storage area, this is a relatively special storage area, where constant is stored, is not allowed to modify (of course, you have to modify the non-normal means, and there are many ways, in "constegy", I A six ways to clearly distinguish between bits and stacks on BBS, the difference between the stack and stack, seems to be an eternal topic, which can be seen 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; management method: 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. 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.

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

New Post(0)