auto_ptr

This class template is deprecated as of C++11. unique_ptr is a new facility with similar functionality, but with improved security. auto_ptr is a smart pointer that manages an object obtained via a new expression and deletes that object when auto_ptr itself is destroyed. An object when described using the auto_ptr class it stores a pointer to a single allocated object which ensures that when it goes out of scope, the object it points to must get automatically destroyed. It is based on an exclusive ownership model i.e. two pointers of the same type can’t point to the same resource at the same time. As shown in the below program, copying or assigning pointers changes the ownership i.e. source pointer has to give ownership to the destination pointer.

Example: 

C++




// C++ program to demonstrate the use of auto_ptr
// C++ program to illustrate the use of auto_ptr
#include <iostream>
#include <memory>
using namespace std;
  
class A {
public:
    void show() { cout << "A::show()" << endl; }
};
  
int main()
{
    // p1 is an auto_ptr of type A
    auto_ptr<A> p1(new A);
    p1->show();
  
    // returns the memory address of p1
    cout << p1.get() << endl;
  
    // copy constructor called, this makes p1 empty.
    auto_ptr<A> p2(p1);
    p2->show();
  
    // p1 is empty now
    cout << p1.get() << endl;
  
    // p1 gets copied in p2
    cout << p2.get() << endl;
  
    return 0;
}


Output:

A::show()
0x1b42c20
A::show()
0          
0x1b42c20

The copy constructor and the assignment operator of auto_ptr do not actually copy the stored pointer instead they transfer it, leaving the first auto_ptr object empty. This was one way to implement strict ownership so that only one auto_ptr object can own the pointer at any given time i.e. auto_ptr should not be used where copy semantics are needed.

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.  

auto_ptr vs unique_ptr vs shared_ptr vs weak_ptr in C++

Prerequisite Smart Pointers

Smart Pointer is a  pointer-wrapping stack-allocated object. Smart pointers, in plain terms, are classes that wrap a pointer, or scoped pointers.

C++ libraries provide implementations of smart pointers in the following types:

  • auto_ptr
  • unique_ptr
  • shared_ptr
  • weak_ptr

They all are declared in a memory header file( #include<memory>) which is used to manage dynamic memory allocation.  

Similar Reads

auto_ptr

This class template is deprecated as of C++11. unique_ptr is a new facility with similar functionality, but with improved security. auto_ptr is a smart pointer that manages an object obtained via a new expression and deletes that object when auto_ptr itself is destroyed. An object when described using the auto_ptr class it stores a pointer to a single allocated object which ensures that when it goes out of scope, the object it points to must get automatically destroyed. It is based on an exclusive ownership model i.e. two pointers of the same type can’t point to the same resource at the same time. As shown in the below program, copying or assigning pointers changes the ownership i.e. source pointer has to give ownership to the destination pointer....

unique_ptr

...

When to use unique_ptr?

std::unique_ptr was developed in C++11 as a replacement for std::auto_ptr. unique_ptr is a new facility with similar functionality, but with improved security (no fake copy assignments), added features (deleters), and support for arrays. It is a container for raw pointers. It explicitly prevents copying of its contained pointer as would happen with a normal assignment i.e. it allows exactly one owner of the underlying pointer. So, when using unique_ptr there can only be at most one unique_ptr at any one resource and when that unique_ptr is destroyed, the resource is automatically claimed. Also, since there can only be one unique_ptr to any resource, any attempt to make a copy of unique_ptr will cause a compile-time error....

shared_ptr

...

When to use shared_ptr?

Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for a single resource it’s not possible to copy one unique_ptr to another....

weak_ptr

A shared_ptr is a container for raw pointers. It is a reference counting ownership model i.e. it maintains the reference count of its contained pointer in cooperation with all copies of the shared_ptr. So, the counter is incremented each time a new pointer points to the resource and decremented when the destructor of the object is called....

When to use weak_ptr?

...