Effective C + + notes three resources management

Source: Internet
Author: User

Article 13: Managing Resources with objects

Many resources are allocated dynamically within the heap and are then used within a single chunk or function. They should be released when the control flow leaves that block or function. The auto_ptr provided by the standard library is a specially crafted product designed for this form. Auto_ptr is a class pointer object, which is a smart pointer whose destructor automatically calls delete on the object it refers to.

Two ideas for managing resources with objects: when resources are obtained, they are immediately placed within the management object; Management objects use destructors to ensure that resources are freed.

If you copy them by using the copy constructor or the copy assignment operator, they become null, and the copied pointer gets the unique ownership of the resource.

The alternative to auto_ptr is a reference counting smart pointer (reference-counting smart pointer; RCSP), keeps track of how many objects are pointing to a resource and automatically deletes the resource when no one points to them. Rcsps provides behavior similar to garbage collection, unlike Rcsps that cannot break a ring reference.

Remember:

To prevent resource leaks, use the Raii object, which obtains resources in constructors and frees resources in destructors.

The two commonly used RAII classes are tr1::shared_ptr and auto_ptr respectively. The former is generally preferred because its copy behavior is more intuitive. If you choose Auto_ptr, the Copy action will point the object to null.

Article 14: Beware of copying behavior in resource management classes

What happens when a Raii object is copied?

    • Copying is forbidden. Many times it is not reasonable to allow RAII objects to be copied. Because it is very rare to have a copy of a synchronized base artifact reasonably. If the copy action is not reasonable for RAII class, you should prohibit it.
    • A reference counting method for the underlying resource. Sometimes we want to keep resources until the last user of it is destroyed. When you copy a Raii object in this case, you should increment the referenced number of the resource.
    • Copy the bottom resource. You can have any number of copies for a resource. When copying a resource management object, make a deep copy.
    • Transfer ownership of the bottom resource. Want to make sure that there is always only one Raii object pointing to an unprocessed resource, even if the Raii object is copied. At this point the ownership of the resource is transferred from the copied object to the target.

Remember:

Copying a Raii object must replicate the resources it manages, so the copying behavior of the resource determines the copying behavior of the Raii object.

The general and common RAII class copying behavior is: Suppress copying, execute reference counting method. But other behaviors can also be achieved.

Article 15: Provide access to the original resource in the resource management class

Resource management classes are great, they are your bulwark against resource leaks. The elimination of such leaks is a fundamental property of a good design system.

Many APIs directly refer to resources and have to bypass resource management objects to directly access the original resources.

There are two ways to achieve a goal: explicit and implicit conversions.

Both Tr1::shared_ptr and Auto_ptr provide a GET member function to perform an explicit conversion, that is, it returns the original pointer inside the smart pointer. Smart pointers also overload the pointer-to-value operators (operator-> and operator*), which allow implicit conversion to the bottom raw pointer.

Sometimes it is necessary to obtain the primitive resources within the Raii object, by providing an implicit conversion function.

Whether to provide an explicit conversion function to convert RAII class to its bottom resource, or to provide an implicit conversion, the answer depends primarily on the specific work that RAII class is designed to perform, and the situation in which it is used.

Remember:

APIs often require access to raw resources, so each RAII class should provide a way to get the resources it manages.

Article 16: Paired use new and delete to take the same form

When you use new to generate an object dynamically through new, two things happen. First, the memory is allocated. Second, one or more constructors are called for this memory. When you use Delete, two things happen: one or more destructors are called for this memory, and then the memory is freed. The biggest problem with delete is: How many objects are there in the memory that will be deleted? The answer to this question determines how many destructors must be called.

When you compose a class that contains a pointer to a dynamic memory allocation and provide multiple constructors, you must be careful to initialize the pointer members with the same form of new in all constructors.

It is better not to do typedefs action in the form of arrays.

Remember:

If you use [] in a new expression, you must also use [] in the corresponding delete expression. If you do not use [] in the new expression, you must not use [] in the corresponding delete expression.

Article 17: Placing a Newed object into a smart pointer with a standalone statement

The compiler does not complete the accounting of function parameters in a particular order.

Detach the statement because the compiler has no freedom to rearrange the actions of the statements that span the statement.

Remember:

Stores the Newed object in a smart pointer in a separate statement. If you do not, once an exception is thrown, it is possible to cause an imperceptible resource leak.

Effective C + + notes three resources management

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.