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?

2
  • possible duplicate of Why isn't there a std::shared_ptr<T[]> specialisation? Commented May 11, 2013 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. Commented May 11, 2013 at 13:36

2 Answers 2

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[]>());
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.