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

时间秒杀一切 提交于 2019-12-30 08:23:15

问题


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...


回答1:


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);



回答2:


T* doesn't contain such information, neither unique_ptr knows about the size of the array (since it uses directly a delete [] as you stated). You could let the T be a unique_ptr<T> to manage the destruction automatically but this could not be possible if the whole contiguous T* is managed by a memory allocator (and not a single T* object). Eg:

unique_ptr<unique_ptr<Foo>[]> data;
data.reset(new unique_ptr<Foo>[50]);
data[0].reset(new Foo());


来源:https://stackoverflow.com/questions/28413035/stdunique-ptrt-and-custom-allocator-deleter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!