Effective use of Auto

xiaoxiao2021-03-06  39

Translation: Elmar

This article was published in October 1999 C / C Users Journal, 17 (10)

Many people have heard of standard auto_ptr intelligent pointer mechanism, but not everyone uses it every day. This is a regret because Auto_Ptr elegantly solves the common problems in C design and coding, correctly uses it to generate a robust code. This article describes how to properly use Auto_PTR to make your code more secure - and how to avoid Danger but common misuse, these misuse will initiate inter-breakfast, difficult to diagnose bugs.

1. Why is it called "automatic" pointer? Auto_PTR is just one of many possible smart pointers. Many commercial libraries offer more complex intelligent pointers, which are widely used, from the number of management references to provide advanced proxy services. You can regard standard C auto_ptr as a smart pointer for the intelligent pointer (ELMAR Note: a model of a Fort-fitting model): a simple, universal intelligent pointer, does not contain all tips, unlike dedicated or high Performance smart pointer is so luxurious, but it can complete many universal work well, it is very suitable for daily use.

What Auto_PTR is done is dynamically assigned objects and automatically perform cleaning when an object is no longer needed. Here is a simple code example, not using auto_ptr, so unsafe: // Example 1 (a): Original code // void f () {t * pt (new T);

/ * ... more code ... * /

delete pt;}

Most people write similar code every day. If the F () function is only three rows and there will be no accidents, which may be very good. But if f () does not execute the DELETE statement, or because of the premature return, or since the abnormality is thrown when the function body is executed, then this assigned object is not deleted, so that we have a classic memory. leakage.

It can make examples 1 (a) secure simple ways to package pointer in an "smart" similar to the pointer, this object has this pointer and can automatically delete the object referred to in this pointer when the destructor. Because this smart pointer can be simply as an automatic object (this is said that it will be automatically destroyed when it goes out of the scope), so it is naturally called "intelligent" pointer:

// Example 1 (b): Security code, use auto_ptr // void f () {auto_ptr PT (New T);

/ * ... more code ... * /

} // Cool: When the PT has a function of the scope, the function is called, //, the object is automatically deleted

Now the code does not leak T type objects, regardless of this function is normal exit or throwing an exception, because PT's destructive function always calls when it is out of the stack. Clean up automatically.

Finally, using an auto_ptr just like using a built-in pointer, and if you want to "revoke" resources, re-use manual ownership, we only call release ():

// Example 2: Using an auto_ptr // void g () {t * pt1 = new t; // Now we have an allocated object

// passed all the rights to an Auto_PTR object Auto_PTR PT2 (PT1);

// Use auto_ptr just like we used to use a simple pointer * PT2 = 12; // is like "* pt1 = 12;" PT2-> somefunc (); // is like "pt1-> somefunc ();"

// Use get () to get the value of the pointer assert (pt1 == pt2.get ()); // Using Release () to undo ownership T * pt3 = pt2.release ();

// I delete this object, because now // No auto_ptr has this object delete PT3;

} // PT2 no longer has any pointer, so don't / / try to delete it ... OK, don't repeat delete

Finally, we can use Auto_PTR's reset () function to reset Auto_PTR to have another object. If this auto_ptr already has an object, then it will first delete the object already owned, so calling reset () as destroying this auto_ptr, then create a new object:

// Example 3: Using reset () // void h () {auto_ptr Pt (New T (1));

Pt.reset (New T (2)); // Delete the first T assigned by "New T (1)"

} // Finally, the PT has a role domain, // The second T is also deleted.

(to be continued)

转载请注明原文地址:https://www.9cbs.com/read-77811.html

New Post(0)