Boost smart pointer-shared_ptr

Source: Internet
Author: User

Although boost: scoped_ptr is easy to use, its ability to share ownership is greatly limited, while boost: shared_ptr can solve this limitation. As the name implies, boost: shared_ptr is a smart pointer that can share ownership. First, let's look at its basic usage through an example:

 

#include <string>#include <iostream>#include <boost/shared_ptr.hpp>class implementation{public:    ~implementation() { std::cout <<"destroying implementation\n"; }    void do_something() { std::cout << "did something\n"; }};void test(){    boost::shared_ptr<implementation> sp1(new implementation());    std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n";    boost::shared_ptr<implementation> sp2 = sp1;    std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n";        sp1.reset();    std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references\n";    sp2.reset();    std::cout<<"After Reset sp2.\n";}void main(){    test();} 

 

The output result of this program is as follows:

The sample now has 1 References
The sample now has 2 references
After reset SP1. the sample now has 1 References
Destroying implementation
After reset SP2.

We can see that the boost: shared_ptr pointer SP1 and SP2 both have the permission to access the implementation object, and when both SP1 and SP2 release the ownership of this object, the memory of the managed object is automatically released. The shared object's access permissions also enable automatic management of its memory.

Boost: Memory Management Mechanism of shared_ptr:

The Management Mechanism of Boost: shared_ptr is not complicated. It refers to the reference count of the managed objects. When a boost: shared_ptr is added to manage the object, add one to the reference count of the object. When a boost: shared_ptr is used to manage the object, the reference count of the object is reduced by one, if the reference count of this object is 0, it means that there is no pointer to manage it, and delete is called to release the memory occupied by it.

The example above can be illustrated as follows:

  1. SP1 manages implementation objects, and its reference count is 1
  2. Increase SP2 to manage implementation objects, and increase the reference count to 2.
  3. SP1 releases implementation object management, and its reference count is changed to 1
  4. The implementation object is managed when SP2 is released. The reference count of the implementation object is 0, and the object is automatically deleted.

Boost: shared_ptr features:

Compared with the boost: scoped_ptr introduced earlier, boost: shared_ptr can share the ownership of objects, so there is basically no limit on the scope of use (there are still some rules to follow, as described below), it can also be used in STL containers. In addition, it is thread-safe, which is also very important in multi-threaded programs.

Rules for using boost: shared_ptr:

Boost: shared_ptr is not absolutely secure. The following rules allow us to use boost: shared_ptr more securely:

  1. Avoid direct memory management operations on the objects managed by shared_ptr, so as to avoid re-release of the objects.
  2. Shared_ptr cannot automatically manage the memory of objects that are referenced cyclically (this is a common problem of other memory management methods for reference count ).
  3. Do not construct a temporary shared_ptr as a function parameter.
    The following code may cause memory leakage:
    Void test ()
    {
    Foo (boost: shared_ptr <implementation> (new implementation (), g ());
    }
    When the function g () throws an exception, it will be leaked. This is the standard bad practices specified in the boost document. Correct usage:
    Void test ()
    {
    Boost: shared_ptr <implementation> Sp (New
    Implementation ());
    Foo (SP, g ());
    }

 

Address: http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html

 

 

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.