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.
The auto_ptr
deprecated, replacing it with the unique_ptr
In computer science, a smart pointer is an abstract data type that simulates a Pointer (computer programming), pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bu ...
class template. auto_ptr
was fully removed in shared_ptr
template class can be used. shared_ptr
was defined in Declaration
Theauto_ptr
class is declared in ISO/IEC 14882, section 20.4.5 as:
Semantics
Theauto_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:
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 ">/a>/code> 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.
See also
* Smart pointer
* Generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
References
External links
Using auto_ptr
effectively
* Article
Using the auto_ptr
Class Template to Facilitate Dynamic Memory Management
by Danny Kalev
* Article
Container of auto_ptr
by Zeeshan Amjad
* Article
Update on auto_ptr
by Scott Meyers
Scott Douglas Meyers (born April 9, 1959) is an American author and software consultant, specializing in the C++ computer programming language. He is known for his ''Effective C++'' book series. During his career, he was a frequent speaker at co ...
auto_ptr
Class Template Reference from GNU libstdc++
{{C++ programming language
Articles with example C++ code
Articles with underscores in the title
C++ Standard Library