Use of auto_ptr

Source: Internet
Author: User

# Include <memory>
# Include <string>
# Include <iostream>
Using namespace STD;

Int main (INT argc, char * argv [])
{
Auto_ptr <int> API, api2;
 
API = auto_ptr <int> (New int (10 ));
Cout <"API:" <* API <Endl;
/*
API object layout: an int number indicates whether the object is valid. (= 1 valid, = 0 invalid)
A pointer points to int data (because you have already said it points to int when declaring an API)
*/
 
Api2 = API; // pass-through: After this sentence is executed, the API is invalid and api2 is valid. Another field of api2 points to int.
Auto_ptr <string> aps1, aps2;
Aps1 = auto_ptr <string> (new string ("pig "));
Cout <"aps1:" <* aps1 <Endl;
 
Return 0;

}

Vc6 description:

Auto_ptr

template<class T>    class auto_ptr {public:    typedef T element_type;    explicit auto_ptr(T *p = 0) throw();    auto_ptr(const auto_ptr<T>& rhs) throw();    auto_ptr<T>& operator=(auto_ptr<T>& rhs) throw();    ~auto_ptr();    T& operator*() const throw();    T *operator->() const throw();    T *get() const throw();    T *release() const throw();    };

The class describes an object that stores a pointer to an allocated object of TypeT. The stored pointer must either be null or designate an object allocated bynewExpression. The object also storesOwnership indicator. An object constructed with a non-null pointer owns the pointer. It transfers ownership if its stored value is assigned to another object. The Destructorauto_ptr<T>Deletes the allocated object if it owns it. Hence, an object of classauto_ptr<T>Ensures that an allocated object is automatically deleted when control leaves a block, even via a thrown exception.

// Well, it is estimated that the auto_ptr functions and shortcomings have been mentioned so much.

// Read the following article.

The auto_ptr pointer in STL is designed to solve the memory leakage problem. It strictly limits the pointer's ownership of the pointing object. The difference between auto_ptr pointer and normal pointer is that the processing of the ownership of the pointing object is different. The auto_ptr pointer is the "pass" ownership, while the normal pointer is the "share" ownership. Take the following example:

STD: auto_ptr <int> P1 (New int (24); STD: auto_ptr <int> P2; int * Q1 = new int (12); int * Q2; p2 = p1; Q2 = Q1; after two assignments, for auto_ptr, P1 is null, * P2 is 24; for common pointers, * P1, * P2 is 12. In the first assignment, P1 transfers the ownership of the object to P2, and P1 no longer has the ownership of the object. In the second assignment, Q2 and Q1 share the ownership of the same object. Therefore, for auto_ptr, an object can only be pointed by a smart pointer, which can effectively avoid Memory leakage. But it will also cause new problems. Take the following example: Template <class T> void badprint (STD: auto_ptr <t> P) {If (P. get () = NULL) {STD: cout <NULL;} else {STD: cout <* P ;}} then I use the badprint function: STD :: auto_ptr <int> q (New int (18); badprint (Q); * q = 12; the program is not as expected: * The Q value is 12, instead, runtime error, why? Since Q is passed as a function parameter to badprint, after the transfer is complete, Q no longer has ownership of the object to which it points, and the local variable P inside the function takes over the ownership of the object to which Q points, when the function exits after execution, P's life cycle ends and delete points to the object. Therefore, Q is actually null, so an error occurs. How to avoid errors? Use auto_ptr reference? That is, void badprint (STD: auto_ptr <t> & P ). This is a pretty bad practice. The use of references to smart pointers obfuscated ownership. It may or may not cause ownership to be transferred. Do not reference auto_ptr in any case. It can be seen that smart pointers are not smart in any situation. Use auto_ptr: 1. The smart pointer cannot share the ownership of the object. 2. The smart pointer cannot point to an array. Because the implementation calls Delete rather than Delete [] 3. Smart pointers are not omnipotent 4. Smart pointers cannot be used as the element of the container class. Example: Template <class T> void container: insert (const T & Value ){.......... X = value ;..........} in fact, in STL, all the container objects to copy the value of the passed parameter internally are the value of the const type. Therefore, it cannot be passed in with auto_ptr., A sentence: // a sentence, suppose there is a dynamically allocated int space, only one auto_ptr Pointer Points to him at a time!
This is the ownership.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.