4

while understanding about auto_ptr, unique_ptr and shared_ptr I came to know that auto_ptr destructor uses delete, not delete[] where as unique_ptr does handle it properly.

auto_ptr<char> aptr(new char[100]);
unique_ptr<char []> uptr(new char[100]);

Anyhow auto_ptr is deprecated in c++11.And I know unique_ptr has much more functionality than auto_ptr. I have two questions related to this behavior

a) Why while designing behavior for auto_ptr by c++ standard library team has not considered it's disadvantages for arrays.

b) Also even though shared_ptr introduced in c++11 why it's implementation doesn't support deleting of array?

shivakumar
  • 3,297
  • 19
  • 28
  • possible duplicate of [Why isn't there a std::shared\_ptr specialisation?](http://stackoverflow.com/questions/8947579/why-isnt-there-a-stdshared-ptrt-specialisation) – Niko May 11 '13 at 13:31
  • You shouldn't be dealing with plain arrays in the first place. you can use a smart pointer to a `std::array` or a `std::vector` or a container of your choice. – stefan May 11 '13 at 13:36

2 Answers2

5

Why while designing behavior for auto_ptr by c++ standard library team has not considered it's disadvantages for arrays.

I can't comment on why auto_ptr wasn't very well designed; I can only observe that it wasn't, which is why it's now deprecated. It's not really worth worrying about; just pretend it never existed.

Also even though shared_ptr introduced in c++11 why it's implementation doesn't support deleting of array?

It supports arbitrary deleters, so you can do that; just slightly less conveniently than with unique_ptr:

std::shared_ptr<int> p(new int[42], std::default_delete<int[]>());
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

Here is a good read on the tortured history of auto_ptr: http://www.aristeia.com/BookErrata/auto_ptr-update.html. The truth is, until rvalue references were invented, there was little hope of designing a bullet proof smart pointer with exception safety for standard containers.

Scott Jones
  • 2,880
  • 13
  • 19