A tr1 Tutorial: Smart pointers

Source: Internet
Author: User
Document directory
  • Class STD: auto_ptr
  • What's new in tr1?

Until tr1, the only smart pointer available in the standard library wasAuto_ptr; That presents some major disadvantages because of its exclusive ownership model. To address these issues, the new standard offers two new implementations,Shared_ptrAndWeak_ptr, Both defined in Header<Memory>.

Class STD: auto_ptr

As mentioned earlier,Auto_ptrIs based on an exclusive ownership model; each means that two pointers (of this type) cannot point to the same resource. copying or assigning makes the resource changing its owner, with the source giving the ownership to the destination.

#include <memory>#include <iostream>class foo{public:   void print() {std::cout << "foo::print" << std::endl;}};int main(){   std::auto_ptr<foo> ap1(new foo);   ap1->print();   std::cout << "ap1 pointer: " << ap1.get() << std::endl;   std::auto_ptr<foo> ap2(ap1);   ap2->print();   std::cout << "ap1 pointer: " << ap1.get() << std::endl;   std::cout << "ap2 pointer: " << ap2.get() << std::endl;   return 0;}

The output is:

foo::printap1 pointer: 0033A790foo::printap1 pointer: 00000000ap2 pointer: 0033A790

The exact value of the wrapped pointer (0033a790) is not important. the issue here is that, after creating and initializing object AP2, AP1 gave up the ownership of the resource, and its wrapper pointer became null.

The major problems introducedAuto_ptrAre:

  • Copying and assigning changes the owner of a resource, modifying not only the destination but also the source, which it not intuitive.
  • It cannot be used in STL containers because the constraint that a container's elements must be copy constructable and assignable does not apply to this class.
What's new in tr1?

Two new smart pointers were added to the standard template library:

  • Shared_ptr: Based on a reference counter model, with the counter incremented each time a new shared pointer object points to the resource, and decremented when the object's destructor executes; when the counter gets to 0, the resource is released. this pointer is copy constructable and assignable; this makes it usable in STL containers. moreover, the shared pointer works with polymorphic types and incomplete types. its major drawback is the impossibility to detect cyclic dependencies, in which case the resources never get released (for example, a tree with nodes having (shared) pointers to children but also to the parent, in which case the parent and the children are referencing each other, in a cycle ). to fix this issue, a second smart pointer was created:
  • Weak_ptr: Points to a resource referred by a shared pointer, but does not participant in reference counting. when the counters gets to 0, the resource is released, regardless the number of weak pointers referring it; all these pointers are marked as invalid.

The next example shows a similar implementation to the first example, replacingAuto_ptrWithShared_ptr.

int main(){   std::tr1::shared_ptr<foo> sp1(new foo);   sp1->print();   std::cout << "sp1 pointer: " << sp1.get() << std::endl;   std::tr1::shared_ptr<foo> sp2(sp1);   sp2->print();   std::cout << "sp1 pointer: " << sp1.get() << std::endl;   std::cout << "sp2 pointer: " << sp2.get() << std::endl;   std::cout << "counter sp1: " << sp1.use_count() << std::endl;   std::cout << "counter sp2: " << sp2.use_count() << std::endl;   return 0;}
foo::printsp1 pointer: 0033A790foo::printsp1 pointer: 0033A790sp2 pointer: 0033A790counter sp1: 2counter sp2: 2

As you can see, when SP2 is created, SP1 does not give up the ownership, changing its wrapped pointer to NULL; it only increments the reference counter. when the two shared pointer objects get out of scope, the last one that is destroyed will release the resource.

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.