[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppAutoPtr

(C++) std::auto_ptr

A smart pointer that deletes the instance it points to when going out of scope. It is supplied in the STL, in the header file memory.h.

It helps to: Note: when you use a lot of forward declarations, you might want to prefer boost::scoped_ptr as it uses boost::checked_delete. Also, boost::scoped_ptr cannot be copied, so you will nearly ever be amazed by 'strange' behaviour.

Overview

Managing pointers and preventing memory leaks

Standard pointer:
{
  MyClass * pClass = new MyClass;
  pClass->doStuff();
  delete pClass
}


Using an auto_ptr:
  1. include <memory>
{ const std::auto_ptr<MyClass> pClass(new MyClass); pClass->doStuff(); //Hey, the same way of accessing the pointed instance! //Done, std::auto_ptr deletes itself when going out of scope }


Use objects to manage resources (like dynamically allocated memory)[2] and store newed objects in smart pointers in standalone statements [3].

std::auto_ptr's does more then saving you a delete statement. It manages that the instance pointed to is only pointed to once. This is done by a non-symettric copy: when you pass a pointer from auto_ptr to auto_ptr, the original possessor gets 0. This is demonstrated below:

  1. include <memory>
  2. include <cassert>
int main() { std::auto_ptr<MyClass> pClass1(new MyClass); std::auto_ptr<MyClass> pClassCopy; assert(pClass1.get()!=0); assert(pClassCopy.get()==0); pClassCopy = pClass1; //Copies the MyClass* assert(pClass1.get()==0); assert(pClassCopy.get()!=0); }


To get a copy to the pointed instance use the get method:

{
  std::auto_ptr<MyClass> pClass(new MyClass);
  MyClass * pClassCopy = pClass.get();
  assert(pClass.get()!=0);
  assert(pClassCopy.get()!=0);
}


Pointer management

Also, using a std::auto_ptr, it gets clear which class manages the deletion of the pointed instance.

class Something{};
class MyClass
{
  std::auto_ptr<Something> pSomething;
  Something * getSomethingCopy() const; 
  std::auto_ptr<Something> getSomething();
};


If the instance pointed to needs to be give to another class, then getSomething is left with an empty (that is: 0) pSomething.

== Make code exception safe == When you create a new instance dynamically in a certain function using a plain pointer, in the end of this function you call delete. But when in the middle an exception is thrown, this delete is not called anymore! When using an auto_ptr, the instance DOES get deleted. This is because auto_ptr's delete their instances when they go out of scope.

== Note, warnings, curiosities ==

=== 1. Do not create an array dynamically using a std::auto_ptr === This will give you a memory leak, as a std::auto_ptr calls the delete statement automatically, instead of delete[]. As advised by [1], you should prefer a std::vector over an array. But if you really want to use a smart pointer, use a scoped_array.

2. Do not put std::auto_ptr's in a std::vector [4]

A copy of a std::auto_ptr does not copy the memory address pointed to. Therefore, when using e.g. a sorting algorithm, some pointed instances get deleted! Instead, use a boost::shared_ptr.

=== 3. Resetting an auto_ptr ===

Resetting an auto_ptr first constructs a new instance of the class before deleting the old instance. This is demonstrated by the code below:

  1. include <iostream>
  2. include <memory>
class Resetter { public: 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); }


This yields the output:
Constructor
Constructor
Destructor
Destructor


== Code links == == Reference ==

last edited (November 9, 2006) by bilderbikkel, Number of views: 18389, Current Rev: 35 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.