In the C++ programming language, auto_ptr is an obsolete smart pointer class template that was available in previous versions of the C++ standard library (declared in the <memory>
header file), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr
class.
The auto_ptr
template class describes an object that stores a pointer to a single allocated object that ensures that the object to which it points gets destroyed automatically when control leaves a scope. [1]
The characteristics of auto_ptr
are now considered unsatisfactory: it was introduced before C++11's move semantics, so it uses copying for what should be done with moves (and confusingly sets the copied-from auto_ptr
to a null pointer). These copy semantics mean that it cannot be used in STL containers. [2]
The C++11 standard made auto_ptr
deprecated, replacing it with the unique_ptr
class template. [3] [4] auto_ptr
was fully removed in C++17. [5] For shared ownership, the shared_ptr
template class can be used. shared_ptr
was defined in C++11 and is also available in the Boost library for use with previous C++ versions. [6]
The auto_ptr
class is declared in ISO/IEC 14882, section 20.4.5 as:
namespacestd{template<classY>structauto_ptr_ref{};template<classX>classauto_ptr{public:usingelement_type=X;// 20.4.5.1 construct/copy/destroy:explicitauto_ptr(X*p=0)throw();auto_ptr(auto_ptr&)throw();template<classY>auto_ptr(auto_ptr<Y>&)throw();auto_ptr&operator=(auto_ptr&)throw();template<classY>auto_ptr&operator=(auto_ptr<Y>&)throw();auto_ptr&operator=(auto_ptr_ref<X>)throw();~auto_ptr()throw();// 20.4.5.2 members:X&operator*()constthrow();X*operator->()constthrow();X*get()constthrow();X*release()throw();voidreset(X*p=0)throw();// 20.4.5.3 conversions:auto_ptr(auto_ptr_ref<X>)throw();template<classY>operatorauto_ptr_ref<Y>()throw();template<classY>operatorauto_ptr<Y>()throw();};}
The auto_ptr
has semantics of strict ownership, meaning that the auto_ptr
instance is the sole entity responsible for the object's lifetime. If an auto_ptr
is copied, the source loses the reference. For example:
#include<iostream>#include<memory>usingstd::auto_ptr;intmain(intargc,char*argv[]){int*a=newint[10];auto_ptr<int[]>x(a);auto_ptr<int[]>y;y=x;std::cout<<x.get()<<std::endl;// Print NULLstd::cout<<y.get()<<std::endl;// Print non-NULL address of areturn0;}
This code will print a NULL address for the first auto_ptr
object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=
). The raw pointer i
in the example should not be deleted, as it will be deleted by the auto_ptr
that owns the reference. In fact, new int
could be passed directly into x
, eliminating the need for i
.
Notice that the object pointed by an auto_ptr
is destroyed using operator delete
; this means that you should only use auto_ptr
for pointers obtained with operator new
. This excludes pointers returned by malloc/calloc/realloc
, and pointers to arrays (because arrays are allocated by operator new[]
and must be deallocated by operator delete[]
).
Because of its copy semantics, auto_ptr
may not be used in STL containers that may perform element copies in their operations.
{{cite book}}
: CS1 maint: location missing publisher (link)auto_ptr
effectively auto_ptr
auto_ptr
Class Template to Facilitate Dynamic Memory Management" by Danny Kalev auto_ptr
" by Zeeshan Amjad auto_ptr
" by Scott Meyers auto_ptr
Class Template Reference from GNU libstdc++ auto_ptr
reference from Rogue Wave