Auto ptr


Auto ptr

auto_ptr is a template class available in the C++ Standard Library (declared in ) that provides some basic RAII features for C++ raw pointers.

The auto_ptr template class describes an object that stores a pointer to an allocated object of type Type* that ensures that the object to which it points gets destroyed automatically when control leaves a scope. [cite web |url=http://msdn2.microsoft.com/en-us/library/ew3fk483.aspx |publisher=Microsoft |title=auto_ptr Class |accessdate=2006-09-27]

The shared_ptr template class proposed in Technical Report 1 and available in the Boost library can be used as an alternative to auto_ptr for collections with ownership semantics. [cite web |url=http://www.ddj.com/dept/cpp/184401839 |publisher=Dr. Dobb's |title=Collecting Shared Objects |date=2004-07-01 |accessdate=2006-09-27]

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: typedef X element_type;

// 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 r) 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(); };

}

emantics

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 using namespace std;

int *i = new int;auto_ptr x(i);auto_ptr y;

y = x;

cout << x.get() << endl;cout << y.get() << endl;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.

Notice that the object pointed by an auto_ptr is destructed 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 operator new [] .

ee also

* Smart pointer

References

External links

* [http://www.gotw.ca/publications/using_auto_ptr_effectively.htm Using auto_ptr effectively]
* [http://cprogramming.com/tutorial/auto_ptr.html Avoiding Memory Leaks with auto_ptr]
* Article " [http://gethelp.devx.com/techtips/cpp_pro/10min/10min1199.asp Using the auto_ptr Class Template to Facilitate Dynamic Memory Management] " by Danny Kalev
* Article " [http://codeproject.com/KB/cpp/COAP.aspx Container of auto_ptr] " by Zeeshan Amjad
* [http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.0/classstd_1_1auto__ptr.html auto_ptr Class Template Reference from GNU libstdc++]
* [http://www.roguewave.com/support/docs/sourcepro/edition9/html/stdlibref/auto-ptr.html auto_ptr reference from Rogue Wave]


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Pointer (computing) — This article is about the programming data type. For the input interface (for example a computer mouse), see Pointing device. Pointer a pointing to the memory address associated with variable b. Note that in this particular diagram, the computing …   Wikipedia

  • Manual memory management — In computer science, manual memory management refers to the usage of manual instructions by the programmer to identify and deallocate unused objects, or garbage. Up until the mid 1990s, the majority of programming languages used in industry… …   Wikipedia

  • PDP-8 — A PDP 8 on display at the Smithsonian s National Museum of American History in Washington, D.C.. This example is from the first generation of PDP 8s, built with discrete transistors and later known as the Straight 8. The 12 bit PDP 8 was the… …   Wikipedia

  • C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… …   Wikipedia

  • Domain Name System — Pour les articles homonymes, voir DNS. Domain Name System Fonction Traduction de nom de domaine en adresse IP …   Wikipédia en Français

  • Domain Name Server — Domain Name System Pour les articles homonymes, voir DNS. Pile de protocoles 7 • Application 6 • …   Wikipédia en Français

  • Domain Name Service — Domain Name System Pour les articles homonymes, voir DNS. Pile de protocoles 7 • Application 6 • …   Wikipédia en Français

  • Domain name system — Pour les articles homonymes, voir DNS. Pile de protocoles 7 • Application 6 • …   Wikipédia en Français

  • Résolution des noms — Domain Name System Pour les articles homonymes, voir DNS. Pile de protocoles 7 • Application 6 • …   Wikipédia en Français

  • Serveur DNS — Domain Name System Pour les articles homonymes, voir DNS. Pile de protocoles 7 • Application 6 • …   Wikipédia en Français