Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using auto_ptr<> with array

I'm using auto_ptr<> which uses an array of class pointer type so how do I assign a value to it.

e.g. auto_ptr<class*> arr[10];

How can I assign a value to the arr array?

like image 372
balu Avatar asked Jun 29 '11 12:06

balu


People also ask

Why auto_ptr is deprecated?

Why is auto_ptr deprecated? It takes ownership of the pointer in a way that no two pointers should contain the same object. Assignment transfers ownership and resets the rvalue auto pointer to a null pointer. Thus, they can't be used within STL containers due to the aforementioned inability to be copied.

Is auto_ptr deprecated?

The C++11 standard made auto_ptr deprecated, replacing it with the unique_ptr class template. auto_ptr was fully removed in C++17.

How is auto_ptr implemented?

The auto_ptr transfers the ownership of internal resource during a copy operation. Furthermore, such a behavior happens in both the cases of copy. That is whenever a new auto_ptr is constructed using copy constructor or whenever an assignment operation happens.

What is std :: auto_ptr?

std::auto_ptr auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed.


1 Answers

You cannot use auto_ptr with array, because it calls delete p, not delete [] p.

You want boost::scoped_array or some other boost::smart_array :)

like image 53
Armen Tsirunyan Avatar answered Sep 22 '22 05:09

Armen Tsirunyan