On the C ++ (limitations of comparing new) allocator memory management (rpm)

The STL, alloc design of memory management, forced me to learn the allocator class. I made a point to note here allocator left to their own memory management follow-up inspection. allocator class declaration, defined in the header file <memory> in name space std. Therefore, you should have the following file is located in the head ...

#include <memory>
using namespace std;

Article directories
 1 . We know malloc and new
 2 . C ++ in new limitations
 3 . Use memory allocator will allocate, separated from the object constructor
1 . We know malloc and new Before then, I only know of two ways to open up memory. First, you can use the C language functions malloc, realloc , calloc open up memory, for example: int * PTR = ( int *) the malloc ( 10 * the sizeof ( int )); / * a cast * / Second, you can use C ++ way to open up the memory, such as: int* ptr = new int[10]; For, C-style memory management not discussed here. However, C ++'s new style, here summarize its limitations. For me personally, I usually write this new new int [ 10 ] (), that is, in the final plus one pair of parentheses, because C ++ to ensure that this can be int [ 10 memory all initialized to zero] in . 2 . C ++ in new limitations For more than this passage, there is a need to clarify the topic, that is - the memory configuration. For C ++ the new terms, it first ( 1 ) allocation of memory, and then automatically completed ( 2 ) object construction. "Depth exploration of C ++ can be used here, Mr. Hou Jie translation of the object model," a book of pseudo code to represent the new process: Point * = __new heap ( the sizeof (Point)); // open memory IF (! Head = 0 ) { head -> Point :: Point (); // object constructor (Memory configuration) } Note that, no new represents new __ (it only completes the memory request), throughout the above pseudo-code for the new process functions performed. It is because of this new series of operations, resulting in a decline in performance. such as, auto p = new string[100]; for (int i = 0; i < 5; ++i){ p[i] = "balaba..."; } In fact, I only need five string, while the new 100 objects constructed all good (each string has been initialized to an empty string, which is "" ). Then, in turn followed by P [ 0 - . 4 ] assigned to Balaba ... The front is the p-[ 0 - 4 ] assigned to an empty string operations, become meaningless . 3 . Use allocator allocates memory, object construction separated Now, new has its own limitations. For high performance requirements STL it is certainly not a new use. Fortunately, there is an allocator class - it is also a template class, and is used to treat memory problems. The new memory allocator class assignment, object construction, regarded as two independent processes, by an independent function is responsible. for example: the allocator < char > STR; char * Base = str.allocate ( 10 ), * P = Base ; // Memory allocation str.construct (P ++, ' A ' ); // object to construct and initialize str.construct (P ++, ' B ' ); cout << base[0] << base[1]; Because allocator is a template class, you need to specify the type. Next, call the allocate ( 10 ) function to allocate memory (10 char application memory). Then, using the function configuration construct Base [ 0 ] of this memory, and the initial value assigned to a. This will be new memory allocation, memory configuration to the sub left. Everything, as we have seen. Similarly, the delete process also split off. This is necessary, we can not delete allocate to free the memory allocated. str.destroy ( - P); // destroy objects str.destroy (- the p-); str.deallocate ( Base , 10 ); // release the memory Summary: If you are using allocate memory returned, then it must construct construct it. Otherwise, your subsequent behavior is undefined (also serious consequences). However, there are a few exceptions. They are uninitialized_copy, uninitialized_copy_n, uninitialized_fill and uninitialized_fill_n. From the name to know uninitialized (uninitialized), their parameters must be pointed unconstructed memory, such as at a given position will uninitialized_copy construction elements. Interested can read the "dynamic memory management allocator class C ++ STL Standard Template Library vector achieve the" look at their usage. ———————————————— Disclaimer: This article is the original article CSDN bloggers "qingdujun", and follow CC 4.0 BY- SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: HTTPS: // blog.csdn.net/qingdujun/article/details/85224771

 

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/12032263.html