Auto ptr
Encyclopedia
auto_ptr is a class template
available in the C++
Standard Library
(declared in the
) that provides some basic RAII
features for C++ raw pointers.
The
The
, can be used as an alternative to
The current C++ standard, C++11, made
This code will print a NULL address for the first
Notice that the object pointed by an
Because of its copy semantics,
However, an
Template (programming)
Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one....
available in the C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
Standard Library
C++ standard library
In C++, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself...
(declared in the
header fileHeader file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...
) that provides some basic RAII
Resource Acquisition Is Initialization
Resource Acquisition Is Initialization is a programming idiom used in several object-oriented languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup to deal with resource deallocation in C++...
features for C++ raw pointers.
The
auto_ptr
template class describes an object that stores a pointer to a single allocated object of type Type*
that ensures that the object to which it points gets destroyed automatically when control leaves a scope.The
shared_ptr
template class defined in C++11, and available in the Boost libraryBoost library
Boost is a set of free software libraries that extend the functionality of C++.-Overview:Most of the Boost libraries are licensed under the Boost Software License, designed to allow Boost to be used with both free and proprietary software projects...
, can be used as an alternative to
auto_ptr
for collections with ownership semantics.The current C++ standard, C++11, made
auto_ptr
deprecated, replacing it with the unique_ptr
class template.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: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/reallocMallocC dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....
and arrays, which 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.However, an
auto_ptr
containing an STL container may be used to prevent further modification of the container.External links
- Using
auto_ptr
effectively - Avoiding Memory Leaks with
auto_ptr
- 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 MeyersScott MeyersScott Douglas Meyers is an American author and software consultant, specializing in the C++ computer programming language. He is known for his Effective C++ book series. He is a frequent speaker at conferences and trade shows. He holds a Ph.D. in computer science from Brown University and M.S... -
auto_ptr
Class Template Reference from GNU libstdc++ -
auto_ptr
reference from Rogue Wave