To allocate a char* array I would normally write something like:
char* arr = new char[size];
How can I achieve the same thing using boost::shared_ptr (or probably boost::shared_array) and boost::make_shared?
My guesses are:
1) boost::shared_ptr<char[]> arr = boost::make_shared<char[]>(size);
2) boost::shared_ptr<char> arr = boost::make_shared<char>(size);
3) boost::shared_ptr<char> arr = boost::shared_ptr<char>(new char[size]);
The last one looks right but is it guaranteed that upon destruction delete [] arr will be called?
The correct one is make_shared<T[]>(n)
.
Bear in mind that pre-c++20 it's an extension not present in the standard library (although unique_ptr<T[]>
is part of the standard).
Boost's shared_ptr
will indeed call the correct deleter (naively,
delete[]
) for it.