http://www.codeproject.com/KB/tips/CBP_for_memory_allocation.aspx
Use auto_ptr
more often than you currently do when allocating so that deallocation is handled automatically. Remember the following guidelines when dealing with auto_ptr
s.
- An existing non-const
auto_ptr
can be reassigned to own a different object by using itsreset()
function. - The
auto_ptr::reset()
function deletes the existing owned object before owning the new one. - Only one
auto_ptr
can own an object. So after oneauto_ptr
(say, P1) has been assigned to anotherauto_ptr
(say, P2) do not use P1 any longer to call a method on the object as P1 is reset toNULL
. Remember that the copy of anauto_ptr
is not equivalent to the original. - Do not put
auto_ptr
s into standard containers. This is because doing this creates a copy of theauto_ptr
and as mentioned above, the copy of anauto_ptr
is not equivalent to the original. - Dereferencing an
auto_ptr
is the only allowed operation on a constauto_ptr
. auto_ptr
cannot be used to manage arrays.