将内存分配给std :: shared_ptr的正确方法 [英] Correct way to allocate memory to std::shared_ptr
问题描述
我实现了一个功能,在该功能中,身份被授予我,而我无法控制.它返回std::shared_ptr<const void>
.在该函数中,我分配了任意数量的内存,并通过shared_ptr返回对其的访问.
I have implemented a function where the identity is given to me and out of my control. It returns std::shared_ptr<const void>
. In the function i allocate an arbitrary amount of memory, and return access to it though the shared_ptr.
我的内存分配是通过new unsigned char[123]
完成的.问题是valgrind检测到new和delete变体的用法之间不匹配.当我使用new[](unsigned)
分配内存时,shared_ptr析构函数使用delete(void*)
进行分配,并且当您使用不正确"的分配器进行分配时,valgrind会发出警告.
My memory allocation is done with new unsigned char[123]
. The problem is that valgrind detects a mismatch between the usage of new and delete variants. While i use new[](unsigned)
to allocate memory, the shared_ptr destructor uses delete(void*)
to deallocate it, and valgrind warns whenever you are using the "incorrect" deallocator for an allocation.
更实际地说,我编写了这个测试用例来说明我的意思:
In more practical terms, i wrote this test case to show what i mean:
TEST(Example, Test1)
{
unsigned char* mem = new unsigned char[123];
std::shared_ptr<const void> ptr(mem);
}
valgrind报告说
The valgrind report says
==45794== Mismatched free() / delete / delete []
==45794== at 0x4C2A64B: operator delete(void*) (vg_replace_malloc.c:576)
==45794== by 0x40B7B5: _M_release (shared_ptr_base.h:150)
==45794== by 0x40B7B5: ~__shared_count (shared_ptr_base.h:659)
==45794== by 0x40B7B5: ~__shared_ptr (shared_ptr_base.h:925)
==45794== by 0x40B7B5: ~shared_ptr (shared_ptr.h:93)
==45794== by 0x40B7B5: Example_Test1_Test::TestBody() (test.cc:108)
==45794== Address 0x5cb6290 is 0 bytes inside a block of size 123 alloc'd
==45794== at 0x4C29CAF: operator new[](unsigned long) (vg_replace_malloc.c:423)
==45794== by 0x40B72E: Example_Test1_Test::TestBody() (test.cc:107)
如果可能,我想避免使用valgrind过滤器.
I want to avoid valgrind filters if possible.
分配任意数量的数据并作为std::shared_ptr<const void>
返回的正确方法是什么?
What is the correct way to allocate an arbitrary amount of data and return as a std::shared_ptr<const void>
?
推荐答案
如果为shared_ptr<T>
提供T
指针,则它假定您创建了一个对象,并暗示了删除器delete p;
.如果您的分配实际上是使用new-array执行的,则需要传递执行delete[] p;
的适当删除器.您可以根据需要重复使用std::default_delete
If you give shared_ptr<T>
a T
pointer, it assumes that you created a single object and implies the deleter delete p;
. If your allocation was actually performed with array-new, you need to pass an appropriate deleter that performs delete[] p;
. You can reuse std::default_delete
if you like:
return static_pointer_cast<const void>(
std::shared_ptr<unsigned char>(
new unsigned char[N],
std::default_delete<unsigned char[]>()));
(由于隐含转换,您甚至不需要外部演员表.)
(You don't even need the outer cast, since the conversion is implied.)
在C ++ 17中,shared_ptr
支持数组,因此可以说
In C++17, shared_ptr
supports arrays, so you can say
shared_ptr<unsigned char[]>(new unsigned char[N])
存在并获取正确的删除器(然后转换为空).
there and get the correct deleter (and then convert to void).
这篇关于将内存分配给std :: shared_ptr的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!