Effective C + + reading notes (iii) resource management __c++

Source: Internet
Author: User
3 Resource Management

The so-called resource is, once used it, must return to the system in the future. The most commonly used resource in C + + programs is the dynamic allocation of memory (if you allocate memory but never return it, resulting in memory leaks), but memory is just one of the many resources you have to manage.

Item 13: Managing Resources with objects
Use object to manage resources.

• Putting resources into objects, we can rely on C + + 's destructor automatic invocation mechanism to ensure that resources are freed.

The auto_ptr provided by the STL Standard library is a specially crafted product designed for this form. Auto_ptr is a "class pointer (pointer-like) object", or "smart pointer", whose destructor automatically calls delete on its object.

Std::auto_ptr<investment>pinv (Createinvesment ());

• Get resources immediately into the management object (Managingobject).

Raii (Resource acquisition is initialization)

• Manage Objects (Managingobject) use destructors to ensure that resources are freed.

auto_ptrs copying actions are destructive: If you copy them through the copy constructor or the copyassignment operator, they become null, and the copied pointer gets the sole ownership of the resource. (multiple auto_ptr cannot point to the same object at the same time)

The alternative of auto_ptr is "reference counting-type wisdom pointer" (Reference-countingsmart pointer; RCSP). RCSP is also a smart pointer that keeps track of how many objects are pointing to a resource and automatically deletes the resource when no one points to it. TR1 's tr1::shared_ptr is a RCSP, you can write that.

Std::tr1::shared_ptr<investment>pinv (Createinvestment ());

Both auto_ptr and shared_ptr invoke the delete on their destructor, not the delete action. That means it's wrong to use it on a dynamically allocated array. • Summary

– To prevent resource leaks, use the Raii object, which obtains resources in the constructor and frees resources in the destructor.

– The two commonly used RAII classes are tr1::shared_ptr and auto_ptr respectively. clause 14: Beware of copying behavior in the Resource management category carefully about copying behavior in resource-managing classes.

RAII Code: Resources are obtained during the tectonic period and are released during the period of deconstruction;

• Prohibit copying. Copying Private or inherited uncopyable

• Reference count (Reference-count). SHARED_PTR allows you to specify a so-called "delete" (function or Function object)

Deep Copying

• Transfer the ownership of the bottom resources Summary

– The replication Raii object must replicate the resources it manages, so the copying behavior of the resource determines the copying behavior of the Raii object.

– The common and common RAII class copying behavior is: Inhibition of copying, execution of reference notation (referencecounting). But other actions can also be achieved. clause 15: Provide access to the original resource in the Resource management class provide access to raw resources in resourece-managing classes

• All smart pointers, such as Tr1::shared_ptr and auto_ptr, also overload pointer fetching (pointer dereferencing) operators (operator->) and (operator*), which allow implicit conversions to the bottom of the original pointer; Summary

–api often requires access to the original resource (raw resources), and every RAII class should provide a way to "get the resources it manages."

– Access to the original resource may be through a display conversion or an implicit conversion. It is generally safe to display conversions, but implicit conversions are more convenient for customers. clause 16: In pairs use the same form when new and delete are used Use the sameform in corresponding uses the new and delete.

• When you use new to dynamically generate an object, two things happen;

– Memory is allocated

– One (or more) constructors are invoked for this memory.

• When you use Delete, two things happen:

– One (or more) destructor is invoked for this memory.

– Memory is freed

• Try not to do a TypeDef action in the form of a group. • Summary

– When you build an object with new, use delete []type-object if you use new type-object[], or use delete 17: Place the Newed object into the smart pointer with a standalone statement Store newedobjects in the smart pointers in standalone statements.

• Store the Newed object in the (placed) smart pointer as a standalone statement. If this is not done, once the exception is thrown, it can lead to imperceptible resource leaks.

Std::tr1::shared_tr<widget> PW (new Widget);
The Newed object
processwidget (Pw,priority ()) is stored as a smart pointer within a separate statement.
This call action never as to cause a leak


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.