本文首发于个人博客https://kezunlin.me/post/b82753fc/,欢迎阅读最新内容!
5 methods for c shared_ptr point to an array
Guide
shared_ptr
Prior to C 17
, shared_ptr
could not be used to manage dynamically allocated arrays. By default, shared_ptr
will call delete
on the managed object when no more references remain to it. However, when you allocate using new[]
you need to call delete[]
, and not delete
, to free the resource.
In order to correctly use shared_ptr
with an array, you must supply a custom deleter.
code example
//OK, pointer to int 999
std::shared_ptr<int> sp(new int(999));
template< typename T >
struct array_deleter
{
v