"C + +" smart pointer shared_ptr position Delete (functor)

Source: Internet
Author: User

Smart pointer shared_ptr looks good with reference counting, but there are problems.

1, the reference counting update exists thread safety;

2. Circular Reference--use a weak reference smart pointer (weak_ptr) to break the circular reference (weak_ptr does not increase the reference count)

3. fixed and spatial splitter

For example, opening a file close, using malloc to open up the space, using reference counting to implement the there is a problem.

locking can be resolved when changing the reference count reference count update has thread safety .

Circular reference issues

#include <iostream>using namespace std; #include <memory>//library contains shared_ptrstruct  Listnode{shared_ptr<listnode> _prev;shared_ptr<listnode> _next;~listnode () {cout  <<  "~listnode ()"  << endl;}}; Void  test2 () {shared_ptr<listnode> cur (New listnode);//1shared_ptr<listnode>  next (New listnode);//2cout <<  "cur->"  << cur.use_count ()   << endl;//use_count returns the reference count of shared_ptr cout <<  "next->"  << next.use _count ()  << endl;cur->_next = next;//3next->_prev = cur;//4cout  <<  "cur->"  << cur.use_count ()  << endl;cout <<   "next->"  << next.use_count ()  << endl;} After statements 1, 2, the reference count for cur and next is 1, and the reference count is 2 after statement 1 and 2. However, the last two objects cannot be freed, because the cur is released on the premise that Next is released, and the release of next depends on the release of the Cur, and finally forms a circular reference. Weak_pThe TR (weak reference smart pointer) will do special handling of the reference count to solve this problem. Struct listnode{    weak_ptr<listnode> _prev;    weak_ Ptr<listnode> _next;    ~listnode ()     {         cout <<  "~listnode ()"  << endl;     }};

Fixed-and space-Splitter (PS: The space distributor's fixed special scenario will be used only in this way)

If the pointer is a point to a file type, you can simply close the file in the destructor instead of freeing the space; If the space is created by malloc, the free function is called in the destructor, not the delete operator. The above problem can be solved by imitating function. The specific code is as follows:

Template<class t>class del{public:void operator ()   (const t *ptr) {Delete  ptr;}};/ /functor Implementation FClose and Freestruct fclose{void operator ()   (void* ptr) {cout <<  "Close"  << endl;fclose ((file*) ptr);}; Struct free{void operator ()   (void* ptr) {cout <<  "Free"  <<  Endl;free (PTR);}}; Template<class t,class deleter=del<t>>class sharedptr{public:sharedptr (T*&NBSP;PTR) : _ptr (PTR),  _pcount (New long (1)) {}sharedptr (T* ptr,deleter del): _ptr (PTR),  _pcount ( New long (1)),  _del (del) {}sharedptr<t>& operator= (SHAREDPTR&LT;T&GT;&NBSP;SP) {Swap (_ PTR,&NBSP;SP._PTR); swap (_pcount, sp._pcount); return *this;} ~sharedptr () {cout <<  "~sharedptr ()"  << endl; Release ();} T& operator* () {return *_ptr;} T* getptr ()//Return the original pointer _ptr{return  _ptr;} t* operator-> () {return _ptr;} Void release ()//Release memory {if  (--(*_pcount)  == 0) {_del (_ptr);d Elete _pcount;}} private:t* _ptr;long* _pcount;deleter _del;}; Void test3 () {//custom Remove and Dispenser SHAREDPTR&LT;INT&GT;&NBSP;SP (New int (6)); SHAREDPTR&LT;FILE,&NBSP;FCLOSE&GT;&NBSP;SP1 (fopen ("Test.text",  "W"),  fclose ()); SHAREDPTR&LT;INT,&NBSP;FREE&GT;&NBSP;SP2 ((int*) malloc (sizeof (int) * 6),  free ());

C + + smart pointer shared_ptr location Delete (functor)

Related Article

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.