[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppAutoPtr
It helps to:
Using an auto_ptr:
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:
To get a copy to the pointed instance use the get method:
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.
=== 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:
This yields the output:
== Code links ==
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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:
- Manage pointers and preventing memory leaks.
- Clarifies the pointer management in classes.
- Make code exception safe.
Overview
- Managing pointers and preventing memory leaks
- Clarify the pointer management in classes
- Make code exception safe
- Note, warnings, curiosities
- 1. Do not create an array dynamically using an std::auto_ptr
- 2. Do not put std::auto_ptr's in a std::vector
- 3. Resetting an auto_ptr
- Topic links
- Reference
Managing pointers and preventing memory leaks
Standard pointer:
{
MyClass * pClass = new MyClass;
pClass->doStuff();
delete pClass
}
Using an auto_ptr:
{ 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 }
- include <memory>
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:
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); }
- include <memory>
- include <cassert>
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:
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); }
- include <iostream>
- include <memory>
This yields the output:
Constructor Constructor Destructor Destructor
== Code links ==
- stream out operator '<<'
- scope operator'::'
- #include
- assert
- char
- cout
- endl
- #include
- int
- iostream
- main
- return
- std
- 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. 2004. Chapter 77: 'Use vector and string instead of arrays'
- 2) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. 2005. Item 13: 'Use objects to manage resources'
- 3) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. 2005. Item 17: 'Store newed objects in smart pointers in standalone statements'
- 4) * Scott Meyers. Effective STL. ISBN: 0-201-74962-9. 2001. Item 8: 'Never create containers of auto_ptr's'
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
