Boost smart pointer -- shared_ptrboost smart pointer -- shared_ptr

Source: Internet
Author: User

Boost smart pointer -- shared_ptr-tianfang-blog Garden

Boost smart pointer -- shared_ptr

Boost: scoped_ptrAlthough it is easy to use, its ability to share ownership is greatly limitedBoost: shared_ptrThis limitation can be solved. 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>

ClassImplementation
{
Public:
~ Implementation () {STD: cout <"Destroying implementation \ n";}
VoidDo_something () {STD: cout <"Did something \ n";}
};

VoidTest ()
{
Boost: shared_ptr <implementation> SP1 (NewImplementation ());
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";
}

VoidMain ()
{
Test ();
}

TheProgramThe output result 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 haveImplementationWhen both SP1 and SP2 release ownership of the 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 does not 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 ()
      {< br> Foo (boost: shared_ptr ( New Implementation (), g ());
      }< br> the correct usage is :
      void test ()
      {< br> boost: shared_ptr Sp ( New Implementation ());
      Foo (SP, g ();
      }

Boost: scoped_ptrAlthough it is easy to use, its ability to share ownership is greatly limitedBoost: shared_ptrThis limitation can be solved. 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>

ClassImplementation
{
Public:
~ Implementation () {STD: cout <"Destroying implementation \ n";}
VoidDo_something () {STD: cout <"Did something \ n";}
};

VoidTest ()
{
Boost: shared_ptr <implementation> SP1 (NewImplementation ());
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";
}

VoidMain ()
{
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 haveImplementationWhen both SP1 and SP2 release ownership of the 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 does not 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.
      Memory leakage may occur if the following code is used:
      void test ()
      {< br> Foo (boost: shared_ptr ( New Implementation ()), G ();
      }< br> the correct usage is :
      void test ()
      {< br> boost: shared_ptr Sp ( New Implementation ());
      Foo (SP, g ();
      }

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.