encyclopegia.org

Auto ptr

In the C++ programming language, (for automatic pointer) is an obsolete smart pointer class template that was available in previous versions of the C++ standard library (declared in the 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. 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 standard made auto_ptr deprecated, replacing it with the unique_ptr class template. auto_ptr was fully removed in C++17. 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.

In the C++ programming language, (for automatic pointer) is an obsolete smart pointer class template that was available in previous versions of the C++ standard library (declared in the 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. 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 standard made auto_ptr deprecated, replacing it with the unique_ptr class template. auto_ptr was fully removed in C++17. 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. ==Declaration== The auto_ptr class is declared in ISO/IEC 14882, section 20.4.5 as: namespace std { template struct auto_ptr_ref {}; template class auto_ptr { public: using element_type = X; // 20.4.5.1 construct/copy/destroy: explicit auto_ptr(X* p = 0) throw(); auto_ptr(auto_ptr&) throw(); template auto_ptr(auto_ptr &) throw(); auto_ptr& operator=(auto_ptr&) throw(); template auto_ptr& operator=(auto_ptr &) throw(); auto_ptr& operator=(auto_ptr_ref ) 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 ) throw(); template operator auto_ptr_ref () throw(); template operator auto_ptr () throw(); }; } ==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 std::auto_ptr; int main(int argc, char* argv[]) { int* a = new int[10]; auto_ptr x(a); auto_ptr y; y = x; std::cout 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 a 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 a . 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== Smart pointer Generic programming ==References== ==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 Meyers auto_ptr Class Template Reference from GNU libstdc++ auto_ptr reference from Rogue Wave

References

  1. auto_ptr Class · retrieved 2006-09-26 · rev 1355433056
    Cite this reference

    MLA "auto_ptr Class." 2006-09-26. https://msdn2.microsoft.com/en-us/library/ew3fk483.aspx. Accessed 24 November 2026.

    Chicago "auto_ptr Class." 2006-09-26. https://msdn2.microsoft.com/en-us/library/ew3fk483.aspx.

    APA encyclopegia.org. (2006). auto_ptr Class. Retrieved November 24, 2026, from https://msdn2.microsoft.com/en-us/library/ew3fk483.aspx

  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 ) Wikipedia revision 1355433056, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1355433056
    Cite this reference

    MLA "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 ) Wikipedia revision 1355433056, retrieved 2026-11-18." Meyers, Scott (2014). 2026-11-17. https://en.wikipedia.org/wiki/ISBN_(identifier). Accessed 24 November 2026.

    Chicago "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 ) Wikipedia revision 1355433056, retrieved 2026-11-18." Meyers, Scott (2014). 2026-11-17. https://en.wikipedia.org/wiki/ISBN_(identifier).

    APA Meyers, Scott (2014). (2026). 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 ) Wikipedia revision 1355433056, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/ISBN_(identifier)

  3. Using unique_ptr, Part I · retrieved 2026-11-17 · rev 1355433056
    Cite this reference

    MLA "Using unique_ptr, Part I." Kalev, Danny. 2026-11-17. http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400. Accessed 24 November 2026.

    Chicago "Using unique_ptr, Part I." Kalev, Danny. 2026-11-17. http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400.

    APA Kalev, Danny. (2026). Using unique_ptr, Part I. Retrieved November 24, 2026, from http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=400

  4. Collecting Shared Objects · retrieved 2006-09-26 · rev 1355433056
    Cite this reference

    MLA "Collecting Shared Objects." 2006-09-26. http://www.ddj.com/dept/cpp/184401839. Accessed 24 November 2026.

    Chicago "Collecting Shared Objects." 2006-09-26. http://www.ddj.com/dept/cpp/184401839.

    APA encyclopegia.org. (2006). Collecting Shared Objects. Retrieved November 24, 2026, from http://www.ddj.com/dept/cpp/184401839

  5. Working Draft, Standard for Programming Language C++ N3242 · retrieved 2013-02-16 · rev 1355433056
    Cite this reference

    MLA "Working Draft, Standard for Programming Language C++ N3242." "Working Draft, Standard for Programming Language C++ N3242" (PDF). 2013-02-16. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf. Accessed 24 November 2026.

    Chicago "Working Draft, Standard for Programming Language C++ N3242." "Working Draft, Standard for Programming Language C++ N3242" (PDF). 2013-02-16. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf.

    APA "Working Draft, Standard for Programming Language C++ N3242" (PDF). (2013). Working Draft, Standard for Programming Language C++ N3242. Retrieved November 24, 2026, from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf

  6. Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190 · retrieved 2016-08-28 · rev 1355433056
    Cite this reference

    MLA "Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190." 2016-08-28. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm. Accessed 24 November 2026.

    Chicago "Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190." 2016-08-28. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm.

    APA encyclopegia.org. (2016). Programming Language C++, Library Evolution Working Group JTC1/SC22/WG21 N4190. Retrieved November 24, 2026, from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm

  7. Auto ptr · retrieved 2026-10-19 · rev 1355433056
    Cite this reference

    MLA "Auto ptr." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1355433056. Accessed 24 November 2026.

    Chicago "Auto ptr." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1355433056.

    APA Wikipedia. (2026). Auto ptr. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1355433056

Cite this article

MLA

"Auto ptr." encyclopegia.org, 2026, https://encyclopegia.org/wiki/auto-ptr. Accessed 24 November 2026.

Chicago

"Auto ptr." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/auto-ptr.

APA

encyclopegia.org. (2026). Auto ptr. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/auto-ptr

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Claims

PredicateValueSource
members5.2 membersWikipedia

Related articles

auto racing

stub · references available

Grand Theft Auto V

stub · references available

Grand Theft Auto

stub · references available

Grand Theft Auto III

stub · references available

Grand Theft Auto IV soundtrack releases

stub · references available

PTR

stub · references available

Auto, Motor, Zubehör : AMZ

stub · references available

Auto Focus

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1355433056 · 0.5 · snapshot enwiki-001355433056

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.