Reference counting pointer

Source: Internet
Author: User

As mentioned above in Memory leakage, the reference counting pointer can be copied. Therefore, several copies of a smart pointer can point to the same object. This leads to the question of which copy is responsible for deleting the objects to which they are collectively directed. The answer is that the last extinct smart pointer in this group will delete the object it points. This is similar to the Home Rule: "The last person who leaves the house is responsible for turning off the lights ." To implement this algorithm, these pointers share a counter and record how many smart pointers reference the same object, that is, the term "reference count. Reference counting is widely used: This term simply means that the compiler has a hidden integer variable as a counter. Whenever someone creates a copy of a smart pointer pointing to the target object, the compiler will increase the value of this counter. When any smart pointer is deleted, the compiler reduces the counter value. Therefore, the target object will exist as long as it is necessary. When all smart pointers to it do not exist, the object will be destroyed.

The following is an implementation of referencing a counting pointer:

Template <typename T> class refcountptr {public: explicit refcountptr (T * P = NULL) {create (p);} refcountptr (const refcountptr <t> & RHs) {copy (RHs);} refcountptr <t> & operator = (const refcountptr <t> & RHs) {If (PTR _! = RHS. PTR _) {kill (); copy (RHs);} return * This;} refcountptr <t> & operator = (T * P) {If (PTR _! = P) {kill (); Create (p) ;}return * This ;}~ Refcountptr () {kill ();} t * Get () const {return PTR _;} t * operator-> () const {scpp_test_assert (PTR _! = NULL, "attempt to use operator-> on Mull pointer."); Return PTR _;} T & operator * () const {scpp_test_assert (PTR _! = NULL, "attempt to use operator * on Null Pointer."); return * PTR _;}};

Note that this class also provides copy constructors and value assignment operators, so you can copy these pointers. In this case, the original pointer and the pointer generated by the copy point to the same object (if the original pointer is null, the pointer generated by the copy is also null ). In this sense, their behavior is the same as the regular "primitive" T * pointer. If you no longer need this object, you can "kill" it by assigning the reference counting pointer to null ".

the reference count pointer has some problems. First, creating a non-null real parameter has a large overhead, because the compiler uses the new operator to allocate an integer to the stack, which is a relatively slow operation. Second, referencing the counting pointer is certainly not safe for multithreading. Although multithreading is beyond the scope of this book, it is worth noting. If we are sure that we need to copy the pointer and reasonably confirm that the overhead produced by creating a reference counting pointer can be ignored and the reference counting pointer can be used as opposed to the execution speed of the remaining code.

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.