Reading : The C++ Standard Library - A Tutorial and Reference
I came across with following piece :
Note that the default deleter provided by shared_ptr calls delete, not delete[]. This means that the default deleter is appropriate only if a shared pointer owns a single object created with new. Unfortunately, creating a shared_ptr for an array is possible but wrong:
std::shared_ptr<int> p(new int[10]); // ERROR, but compiles
So, if you use new[] to create an array of objects you have to define your own deleter. You can do that by passing a function, function object, or lambda, which calls delete[ ] for the passed ordinary pointer. For example:
std::shared_ptr<int> p(new int[10],
[](int* p) {
delete[] p;
});
Is there any specific reason behind this limitation? If shared pointer is smart, can't it be that smart that it keeps the info whether it's array or single object?
Though there is a way to do this :
std::shared_ptr<int> p(new int[10], std::default_delete<int[]>());
The issue is that new int[10] and new int return the same type: int*, so there is nothing to do any overloading on. It's a limitation inherited from C, and there isn't much that can be done about it.
Normally you would just use a container instead of directly allocating though, so it isn't usually an issue.
edit: as Richard says, and as mentioned in this question, it seems sensible that std::shared_ptr<int[]> would do as you want, since std::unique_ptr has a similar overload, and storing an actual pointer to an int[] would be kind of redundant (in any case, you could use std::shared_ptr<int*> if you really wanted to).
I still think that using default_deleter<int[]> is the best way to go as it is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With