fbpx
Wikipedia

auto_ptr

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]

Declaration edit

The auto_ptr class is declared in ISO/IEC 14882, section 20.4.5 as:

namespace std {  template <class Y> struct auto_ptr_ref {};  template <class X>  class auto_ptr {  public:  typedef X element_type;  // 20.4.5.1 construct/copy/destroy:  explicit auto_ptr(X* p =0) throw();  auto_ptr(auto_ptr&) throw();  template <class Y> auto_ptr(auto_ptr<Y>&) throw();  auto_ptr& operator=(auto_ptr&) throw();  template <class Y> 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*() const throw();  X* operator->() const throw();  X* get() const throw();  X* release() throw();  void reset(X* p =0) throw();  // 20.4.5.3 conversions:  auto_ptr(auto_ptr_ref<X>) throw();  template <class Y> operator auto_ptr_ref<Y>() throw();  template <class Y> operator auto_ptr<Y>() throw();  }; } 

Semantics edit

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> using namespace std;   int main(int argc, char **argv) {  int *i = new int;  auto_ptr<int> x(i);  auto_ptr<int> y;    y = x;    cout << x.get() << endl; // Print NULL  cout << y.get() << endl; // Print non-NULL address i  return 0; } 

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.

See also edit

References edit

  1. ^ "auto_ptr Class". Microsoft. Retrieved 2006-09-27.
  2. ^ Meyers, Scott (2014). Effective Modern C++: 42 specific ways to improve your use of C++11 and C++14. Sebastopol, CA. p. 118. ISBN 978-1-491-90399-5.{{cite book}}: CS1 maint: location missing publisher (link)
  3. ^ "Working Draft, Standard for Programming Language C++ N3242" (PDF). 28 February 2011. p. 1233. Retrieved 2013-02-17.
  4. ^ Kalev, Danny. "Using unique_ptr, Part I". informIT. Retrieved 30 September 2010.
  5. ^ "Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190". 2014-10-09. Retrieved 2016-08-29.
  6. ^ "Collecting Shared Objects". Dr. Dobb's. 2004-07-01. Retrieved 2006-09-27.

External links edit

  • Using auto_ptr effectively
  • Avoiding Memory Leaks with auto_ptr
  • Article "" 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++

auto, programming, language, obsolete, smart, pointer, class, template, that, available, previous, versions, standard, library, declared, memory, header, file, which, provides, some, basic, raii, features, pointers, been, replaced, unique, class, template, cla. 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 lt memory gt 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 a href Smart pointer html unique ptr title Smart pointer unique ptr a class template 3 4 auto ptr was fully removed in C 17 5 For shared ownership the a href Shared ptr html class mw redirect title Shared ptr shared ptr a 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 Contents 1 Declaration 2 Semantics 3 See also 4 References 5 External linksDeclaration editThe auto ptr class is declared in ISO IEC 14882 section 20 4 5 as namespace std template lt class Y gt struct auto ptr ref template lt class X gt class auto ptr public typedef X element type 20 4 5 1 construct copy destroy explicit auto ptr X p 0 throw auto ptr auto ptr amp throw template lt class Y gt auto ptr auto ptr lt Y gt amp throw auto ptr amp operator auto ptr amp throw template lt class Y gt auto ptr amp operator auto ptr lt Y gt amp throw auto ptr amp operator auto ptr ref lt X gt throw auto ptr throw 20 4 5 2 members X amp operator const throw X operator gt const throw X get const throw X release throw void reset X p 0 throw 20 4 5 3 conversions auto ptr auto ptr ref lt X gt throw template lt class Y gt operator auto ptr ref lt Y gt throw template lt class Y gt operator auto ptr lt Y gt throw Semantics editThe 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 lt iostream gt include lt memory gt using namespace std int main int argc char argv int i new int auto ptr lt int gt x i auto ptr lt int gt y y x cout lt lt x get lt lt endl Print NULL cout lt lt y get lt lt endl Print non NULL address i return 0 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 a href Malloc html class mw redirect title Malloc malloc calloc realloc a and pointers to arrays because arrays are allocated by operator a href New C 2B 2B html class mw redirect title New C new a 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 editSmart pointer Generic programmingReferences edit auto ptr Class Microsoft Retrieved 2006 09 27 Meyers Scott 2014 Effective Modern C 42 specific ways to improve your use of C 11 and C 14 Sebastopol CA p 118 ISBN 978 1 491 90399 5 a href Template Cite book html title Template Cite book cite book a CS1 maint location missing publisher link Working Draft Standard for Programming Language C N3242 PDF 28 February 2011 p 1233 Retrieved 2013 02 17 Kalev Danny Using unique ptr Part I informIT Retrieved 30 September 2010 Programming Language C Library Evolution Working Group JTC1 SC22 WG21 N4190 2014 10 09 Retrieved 2016 08 29 Collecting Shared Objects Dr Dobb s 2004 07 01 Retrieved 2006 09 27 External links editUsing 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 Meyers auto ptr Class Template Reference from GNU libstdc auto ptr reference from Rogue Wave Retrieved from https en wikipedia org w index php title Auto ptr amp oldid 1188331371, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.