In the
C++ programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, auto_ptr is an obsolete
smart pointer
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bugs caused by the misuse of p ...
class
template
Template may refer to:
Tools
* Die (manufacturing), used to cut or shape material
* Mold, in a molding process
* Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs
C ...
that was available in previous versions of the
C++ standard library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C ยง7'' Starting from the origina ...
(declared in the
header file
An include directive instructs a text file processor to replace the directive text with the content of a specified file.
The act of including may be logical in nature. The processor may simply process the include file content at the location of ...
), 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.
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.
The
C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
standard made
auto_ptr
deprecated, replacing it with the
unique_ptr
class template.
auto_ptr
was fully removed in
C++17
C17, C-17 or C.17 may refer to:
Transportation
* , a 1917 British C-class submarine
Air
* Boeing C-17 Globemaster III, a military transport aircraft
* Lockheed Y1C-17 Vega, a six-passenger monoplane
* Cierva C.17, a 1928 English experimental ...
.
For shared ownership, the
shared_ptr
template class can be used.
shared_ptr
was defined in
C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
and is also available in the
Boost library for use with previous C++ versions.
Declaration
The
auto_ptr
class is declared in
ISO/IEC 14882, section 20.4.5 as:
namespace std
Semantics
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
#include
using namespace std;
int main(int argc, char **argv)
This code will print a
NULL
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
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 ">/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 data types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneer ...
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
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