C ++ basics: smart pointer auto_ptr

Source: Internet
Author: User

 

 

 

C ++ smart finger

C ++ skills (Standard auto_ptr smart pointer Mechanism

Many people have heard of the standard auto_ptr smart pointer mechanism, but not everyone uses it every day. This is really a pity, because auto_ptr elegantly solves common problems in C ++ design and coding, and correctly uses it to generate robust code. This article explains how to correctly use auto_ptr to make your code more secure-and how to avoid dangerous but common misuse of auto_ptr, which can cause intermittent onset and difficult to diagnose bugs.

Why is it called an "automatic" pointer?

Auto_ptr is just one of the many possible smart pointers. Many business libraries provide more complex smart pointers for a wide range of uses, from managing the number of references to providing advanced proxy services. We can regard the Standard C ++ auto_ptr as the ford escort of the smart pointer (Elmar Note: it may be a suitable family model for Ford): A simple and general smart pointer, it does not contain all tips, not as luxurious as dedicated or high-performance smart pointers, but it can do a lot of common work well and is suitable for everyday use.

What auto_ptr does is to dynamically allocate objects and automatically clean up objects when they are no longer needed. Here is a simple code example. It is not safe because auto_ptr is not used:

// Example 1 (a): original code
//
Void F ()
{
T * PT (new t );
Delete pt;
}
Most of us write similar code every day. If the F () function has only three rows and there is no accident, it may be good to do so. However, if F () Never executes the delete statement, or is returned prematurely, or an exception is thrown when the function body is executed, the allocated object is not deleted, thus, we have produced a classic memory leakage.

A simple way to make Example 1 (a) safe is to encapsulate pointers in a "smart" object similar to pointers, this object owns this pointer and can automatically delete the object referred to by this pointer during destructor. Because this smart pointer can be simply treated as an automatic object (that is, it will be destroyed automatically when it is out of scope), it is naturally called a "smart" pointer:
// Example 1 (B): security code, using auto_ptr
//
Void F ()
{
Auto_ptr <t> Pt (new t );
} // Cool: When Pt has a scope, the Destructor is called,
// The object is automatically deleted.
At present, the code will not leak T-type objects, no matter whether the function Exits normally or throws an exception, because the PT destructor will always be called at the time of exit of the stack. The cleanup is performed automatically.

Finally, using an auto_ptr is as easy as using a built-in pointer. If you want to "undo" the resource and use manual ownership again, you just need to call release ():
// Example 2: Use an auto_ptr
//
Void g ()
{
T * pt1 = new T;
// Now, we have an allocated object
// Pass ownership to an auto_ptr object
Auto_ptr <t> pt2 (pt1 );
// Use auto_ptr, just like we used simple pointers.
* Pt2 = 12; // It is like "* pt1 = 12 ;"
Pt2-> somefunc (); // like "pt1-> somefunc ();"
// Use get () to obtain the pointer Value
Assert (pt1 = pt2.get ());
// Use release () to revoke ownership
T * pt3 = pt2.release ();
// Delete this object by yourself, because
// No auto_ptr has this object
Delete pt3;
} // Pt2 no longer has any pointer, so do not
// Try to delete it... OK. Do not delete it again
Finally, we can use the reset () function of auto_ptr to reset auto_ptr so that it has another object. If this auto_ptr already has an object, it will first Delete the existing object, so calling reset () is like destroying this auto_ptr, and then creating a new object and having a new object:
// Example 3: Use reset ()
//
Void H ()
{
Auto_ptr <t> Pt (New T (1 ));
PT. Reset (New T (2 ));
// Delete the first t allocated by "new T (1 )"
} // Finally, PT has a scope,
// The second t is also deleted.

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.