Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::unique_ptr<T[]> and custom allocator deleter

I am trying to use std::unique_ptr<T[]> with custom memory allocators. Basically, I have custom allocators that are subclasses of IAllocator, which provides the following methods:

void* Alloc( size_t size )
template<typename T> T* AllocArray( size_t count )
void Free( void* mem )
template<typename T> void FreeArray( T* arr, size_t count )

Since the underlying memory might come from a pre-allocated block, I need the special ...Array()-methods to allocate and free arrays, they allocate/free memory and call T() / ~T() on every element in the range. Now, as far as I know, custom deleters for std::unique_ptr use the signature:

void operator()(T* ptr) const

In the case of unique_ptr<T[]>, normally you would call delete[] and be done with it, but I have to call FreeArray<T>, for which I need the number of elements in the range. Given only the raw pointer, I think there is no way of obtaining the size of the range, hence the only thing I could come up with is this:

std::unique_ptr<T[], MyArrDeleter> somePtr( allocator.AllocArray<T>( 20 ), MyArrDeleter( allocator, 20 ) );

Where essentially the size of the array has to be passed into the deleter object manually. Is there a better way to do this? This seems quite error-prone to me...

like image 334
Mortano Avatar asked Feb 09 '15 15:02

Mortano


People also ask

Does unique_ptr call Delete?

But do remember that unique_ptr are there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.

What is std :: unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

What does unique_ptr Reset do?

unique_ptr::resetReplaces the managed object. 1) Given current_ptr , the pointer that was managed by *this, performs the following actions, in this order: Saves a copy of the current pointer old_ptr = current_ptr. Overwrites the current pointer with the argument current_ptr = ptr.


1 Answers

Yes, there most certainly is a better way:
Use a maker-function.

template<class T, class A> std::unique_ptr<T[], MyArrDeleter>
my_maker(size_t count, A&& allocator) {
    return {somePtr(allocator.AllocArray<T>(count), MyArrDeleter(allocator, count)};
}

auto p = my_maker<T>(42, allocator);
like image 158
Deduplicator Avatar answered Oct 19 '22 05:10

Deduplicator