{
  "id": "enwiki-001355433056",
  "slug": "auto-ptr",
  "title": "Auto ptr",
  "qid": "",
  "lead": "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.",
  "body": "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",
  "categories": [
    "22175909",
    "23118434",
    "23603337",
    "27395026",
    "36327952",
    "52390716"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1355433056",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1779388284.0,
  "claims": [
    {
      "id": 30060447,
      "subject_qid": "",
      "subject_slug": "auto-ptr",
      "predicate": "members",
      "object_text": "5.2 members",
      "object_num": 5.2,
      "object_unit": "members",
      "polarity": 1,
      "qualifier": "",
      "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1355433056",
      "source_name": "Wikipedia",
      "source_tier": 3,
      "retrieved_at": 1792488540.0048711,
      "confidence": 0.95,
      "article_slug": "auto-ptr",
      "span_start": 495,
      "span_end": 506
    }
  ],
  "contradictions": [],
  "confidence": 0.95,
  "csl": {
    "id": "enwiki-001355433056",
    "type": "webpage",
    "title": "Auto ptr",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/auto-ptr",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001355433056",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1355433056",
    "note": "Immutable snapshot enwiki-001355433056"
  }
}