In C + +, dynamic memory can be implemented with new, which is to request space on the heap and to display the release of the dynamic memory. Of course, it can also be implemented with smart pointers.
New has a flaw in the combination of memory allocation and object construction. This is convenient for a single object, but for large chunks of memory, you may need to request space first and then perform the construction successively, and you need to separate the memory allocation from the object construction. This functionality is provided by the allocator class.
Let's take a look at the example of using new:
string New string [ten]; // 10 stirng of space have been requested, and default initialization string s; string *pp = q; while Ten) // Assign values to these strings again *q++ = s; Delete [] Q;
It is visible that the object actually performs one initialization and one assignment at a time, while the actual initialization process is unnecessary.
Next, rewrite it with allocator and then explain the operation of allocator:
allocator<string> alloc;//Assigning a string type of allocatorAuto P = alloc.allocate (Ten);//allocate 10 string spacesAuto Q =p; strings; while(cin >> S&&q! = p +Ten) alloc.construct (q+ +, s);//constructs an element with an input value while(Q! =p) Alloc.destroy (--Q);//destroy elements individuallyAlloc.deallocate (P,Ten);//free up memory space
First, allocator is a template that needs to specify the type of memory to allocate. The main operations of allocator are as follows:
Alloc.allocate (n); // allocate a piece of raw memory, save n elements, return to the first address Alloc.construct (P, args); // in the position of P, call the constructor with args to construct an object Alloc.destroy (P); // call a destructor on a P-pointed object Alloc.deallocate (P, N); // free space, note that n must be equal to the allocated size parameter
There are several places to note:
1. Allocated memory is not constructed, the use of these raw memory is wrong, and can not directly assign these memory!
2. After construction, if you want to release this memory, you need to destroy it first, and you must destroy the constructed element.
3. The size of the allocated and freed memory must be equal.
Because allocator can easily allocate a piece of memory, the standard library provides two sets of functions to create objects for an entire range of areas:
Uninitialized_copy (b, E, B2); // Copy the element between the iterator b,e to the memory in which the pointer B2 starts, B2 memory is large enough Uninitialized_copy_n (b, N, B2); // n elements copied from B to memory beginning with pointer B2 Uninitialized_fill (b, E, t); // As above , the value of the object is a copy of T Uninitialized_fill_n (b, N, T); // As above , the value of the object is a copy of T
However, these functions see some people are not recommended, VS also will be error or warning, because it is provided for internal use of the function, the former recommended to replace with copy.
C + + Allocator