GURU Of The WEEK Terms 09: Memory Management (Part 1)

zhaozj2021-02-08  490

Gotw # 09 Memory Management - Part I

Author: Herb Sutter

Translation: Kingofark

[Declaration]: This article takes the Guru of The Week column on www.gotw.ca website, and its copyright belongs to the original person. Translator Kingofark translated this article without the consent of the original person. This translation is only for self-study and reference, please read this article, do not reprint, spread this translation; people download this translation content, please delete its backup immediately after reading. Translator Kingofark is not responsible for people who violate the above two principles. This declaration.

Revision 1.0

GURU Of The WEEK Terms 09: Memory Management (Part 1)

Difficulty: 3/10

(This Terms Introduce the basic knowledge of several major memory regions in C . Terms 10 will continue to discuss some of memory management in the terms of this clause.)

[problem]

C has several different memory regions to store objects or other types of values. Each area has its own characteristics.

Please call the name of the memory area as possible and analyze the performance features of each area, describe the survival cycle of the object stored therein.

Example: Storage Automatic Variables, including build types, and class objects.

[answer]

The main memory area of ​​the C program is summarized below. Note that some names (such as HEAP) may not be the same as the c standard.

[Const Data "area:]

The constant data area stores a string or the like to be determined during compilation. Class objects cannot exist in this area. Data in the area is available in the entire survival period of the program.

All data in the region is read-only, any act of modifying the behavior of this regional data can cause unpredictable consequences. This is the reason, because in the actual implementation, even the most underlying internal storage format is also subject to the specific optimization scheme implemented. For example, a compiler can completely put the string in several overlapping objects - as long as the implementation is willing.

[Stack ":]

Stack area stores automatic variables. In general, the allocation operation of the stack area is much faster than the dynamic storage area (such as a heap) or free store, because the allocation of the stack area only involves the increment of a pointer, and dynamic The allocation of the storage area involves a more complex management mechanism. In the stack area, once the memory is allocated, the object is immediately constructed; the object is destroyed, the allocated memory is immediately recovered (the authors are used "to match the" DEALLOCATE ", the monks are translated into "Recycling"). Therefore, in the stack area, the programmer has no way to manipulate those stack spaces that have been assigned but have not been initialized (of course, those who deliberately do this by using Explicit Destructor and New Competers. The situation is not enough).

Free store:]

Free Store is one of C two dynamic memory regions, using new and delete to allocate and release (FREED). In the free store, the living cycle of the object can be shorter than the survival cycle of the memory area, which means that we can get a memory area without initializing it immediately; at the same time, in the object After destruction, you don't have to recover it immediately. During this period of the memory area that is destroyed and its occupied, we can access this area through the void * type pointer, but the non-statistial member of its original object (even if we know The address) cannot be accessed or manipulated. [Heap District:]

The heap area is another dynamic storage area, using Malloc, Free, and some correlation variables to allocate and recycle. It should be noted that although the default global operator New and Delete in a specific compiler may be implemented in Malloc and Free, the Heap and the Free Store are different - The memory allocated in a certain area is impossible to be securely reclaimed in another area. The assigned memory in the heap is generally used to store the class objects involved in the concrete process of using the New NEW. The survival cycle of the object in the heap is similar to the Free Store.

[Global / Static Area (Global / Static):]

The overall or static variable and the memory area occupied by the object are allocated when the program startup is startup, and may be initialized until the program begins to execute. For example, the static variable in the function is initialized when the program is executed to the code defined to the variable. The order in which the global variable that spans the translation unit is not explicitly defined, and it is necessary to specify the dependency between the global object (including static objects). Finally, the same as before, there is no initialized object storage area in the global / static area (global / static) to be accessed and manipulated through VOID *, but as long as it is an object's true living cycle, non-static members And member functions cannot be used or referenced.

[About "Heap VS. Free Store"]: In this Terrace, we distinguish between the Heap and the Free Store because of the draft C standards, about this The problem with contact between the two regions has been very cautious. For example, when the memory is recovered through the delete operator, 18.4.1.1 said:

IT IS Unspecified Under What Conditions Part OR All of Such Reclaimed Storage Is Allocated by a Subsequent Call To Operator New Or Any of Calloc, Malloc, Or Realloc, Declared In .

[Regardless of the circumstances, some or all of this memory area will be assigned by the subsequent calls to NEW (or any one of the Calloc, Malloc, Realloc in ). It does not detail here, not detailed. Similarly, in a specific implementation, in the end, New and Delete are implemented in Malloc and Free, or, Malloc and Free are implemented in accordance with New and Delete, which is not conclusive. Simply put, these two memory areas have different ways of operation, and the way access is different - so, of course, it should be used as a different thing!

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

New Post(0)