What is the purpose of the second parameter for std :: allocator <T> :: deallocate?

Here is the mem release declaration. class dispenser. My question is, why the second argument in this declaration? If this function calls the delete (_Ptr) operator, this argument is not used, so why is it?
Thanks.

Excerpt from MSDN:

Flushes the specified number of objects from the storage, starting from the specified position.

void deallocate( pointer _Ptr, size_type _Count ); 

Parameters

_ptr Pointer to the first object to be freed from storage.

_Count The number of objects to be freed from storage.

+4
source share
4 answers

When you call deallocate , you should give it the pointer that you previously received when you called allocate , and the size that you passed to allocate when you originally allocated the memory.

For instance,

 #include <memory> std::allocator<int> a; int* p = a.allocate(42); a.deallocate(p, 42); // the size must match the size passed to allocate 

This is useful for different types of valves. For example, you might have a allocator that uses different pools for blocks of different sizes; such a allocator should know what block size is freed, so that it knows which pool it needs to return memory to.

+7
source

It is not used.

From MSDN :

Resets the specified number of objects from the storage starting from the specified position (_Ptr in this case).

Parameters

_ptr Pointer to the first object to be freed from storage. (starting position)

_Count The number of objects to be freed from storage.

Code example :

 // allocator_allocate.cpp // compile with: /EHsc #include <memory> #include <iostream> #include <vector> using namespace std; int main( ) { allocator<int> v1Alloc; allocator<int>::pointer v1aPtr; v1aPtr = v1Alloc.allocate ( 10 ); int i; for ( i = 0 ; i < 10 ; i++ ) { v1aPtr[ i ] = i; } for ( i = 0 ; i < 10 ; i++ ) { cout << v1aPtr[ i ] << " "; } cout << endl; v1Alloc.deallocate( v1aPtr, 10 ); } 
+5
source

(refer to the example in the documentation for the distributor: http://msdn.microsoft.com/en-us/library/723te7k3.aspx )

I assume that the dispenser does not execute the new [] and does not preserve the number of selected elements, so cannot delete []?

so that the user can tell how many of them need to be allocated and how much to release.

M.

0
source

It seems to me that he expects _Ptr to be a pointer to an array of objects and that it basically does:

 for (size_type i = 0; i<_Count; i++) { delete _Ptr; _Ptr++; } 
0
source

All Articles