smart pointer that deletes the instance it points to when going out of scope
View versions (1)
Definition
The auto_ptr class template is defined in the standard header <memory>, and in the nonstandard backward-compatibility header <memory.h>.
template <class Type>
class auto_ptr
| Parameter | Description |
Type | The type of object for which storage is being allocated or deallocated |
Description
The auto_ptr is provided by the C++ standard library as a kind of a smart pointer that helps to avoid resource leaks when exceptions are thrown.
It manages an object obtained via new and deletes that object when auto_ptr itself is destroyed. It may be used to provide exception safety for dynamically-allocated objects, for passing ownership of dynamically-allocated objects into functions and for returning dynamically-allocated objects from functions.
NOTE:
- Copying an auto_ptr changes the object being copied by setting the contents to NULL
- auto_ptr objects are not guaranteed to work correctly with the STL containers
Member Functions
| Function | Description |
| (constructor) | Creates a new auto_ptr object |
| (destructor) | Destroys an auto_ptr and the managed object |
| get | Obtains a pointer to the managed object |
| operator* | Dereference object (accesses the managed object) |
| operator-> | Dereference object member (accesses the managed object) |
| release | Releases ownership of the managed object |
| reset | Deallocate object pointed and set new value |
| (conversion operators) | Conversion operators |
References
Example 1
ProblemThe following example of program illustrates how to reset an auto_ptr.
Workings#include <iostream>
#include <memory>
struct Resetter
{
Resetter() { std::cout << "Constructor" << std::endl; }
~Resetter() { std::cout << "Destructor" << std::endl; }
};
int main()
{
std::auto_ptr<Resetter> pReset(new Resetter);
pReset.reset(new Resetter);
}
SolutionOutput:
Constructor
Constructor
Destructor
Destructor
Example 2
ProblemThis example of program shows how auto_ptrs behave regarding the transfer of ownership.
Workings#include <iostream>
#include <memory>
using namespace std;
  /* define output operator for auto_ptr
  * - print object value or NULL
  */
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
  //does p own an object ?
 if (p.get() == NULL)
{
   strm << "NULL";     //NO: print NULL
 }
 else
{
   strm << *p;      //YES: print the object
 }
 return strm;
}
int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;
cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
q = p;
cout << "after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
*q += 13; //change value of the object q owns
p = q;
cout << "after change and reassignment:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
}
SolutionOutput:
after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: NULL
q: 42
after change and reassignment:
p: 55
q: NULL
References- Nicolai M. Josuttis: "The C++ Standard Library"