Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7427855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:38:54+00:00 2026-05-29T08:38:54+00:00

I’ve been thinking about using shared pointers, and I know how to implement one

  • 0

I’ve been thinking about using shared pointers, and I know how to implement one myself–Don’t want to do it, so I’m trying std::tr1::shared_ptr,and I have couple of questions…

How is the reference counting implemented? Does it use a doubly linked list? (Btw, I’ve already googled, but I can’t find anything reliable.)

Are there any pitfalls for using the std::tr1::shared_ptr?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:38:56+00:00Added an answer on May 29, 2026 at 8:38 am

    shared_ptr must manage a reference counter and the carrying of a deleter functor that is deduced by the type of the object given at initialization.

    The shared_ptr class typically hosts two members: a T* (that is returned by operator-> and dereferenced in operator*) and a aux* where aux is a inner abstract class that contains:

    • a counter (incremented / decremented upon copy-assign / destroy)
    • whatever is needed to make increment / decrement atomic (not needed if specific platform atomic INC/DEC is available)
    • an abstract virtual destroy()=0;
    • a virtual destructor.

    Such aux class (the actual name depends on the implementation) is derived by a family of templatized classes (parametrized on the type given by the explicit constructor, say U derived from T), that add:

    • a pointer to the object (same as T*, but with the actual type: this is needed to properly manage all the cases of T being a base for whatever U having multiple T in the derivation hierarchy)
    • a copy of the deletor object given as deletion policy to the explicit constructor (or the default deletor just doing delete p, where p is the U* above)
    • the override of the destroy method, calling the deleter functor.

    A simplified sketch can be this one:

    template<class T>
    class shared_ptr
    {
        struct aux
        {
            unsigned count;
    
            aux() :count(1) {}
            virtual void destroy()=0;
            virtual ~aux() {} //must be polymorphic
        };
    
        template<class U, class Deleter>
        struct auximpl: public aux
        {
            U* p;
            Deleter d;
    
            auximpl(U* pu, Deleter x) :p(pu), d(x) {}
            virtual void destroy() { d(p); } 
        };
    
        template<class U>
        struct default_deleter
        {
            void operator()(U* p) const { delete p; }
        };
    
        aux* pa;
        T* pt;
    
        void inc() { if(pa) interlocked_inc(pa->count); }
    
        void dec() 
        { 
            if(pa && !interlocked_dec(pa->count)) 
            {  pa->destroy(); delete pa; }
        }
    
    public:
    
        shared_ptr() :pa(), pt() {}
    
        template<class U, class Deleter>
        shared_ptr(U* pu, Deleter d) :pa(new auximpl<U,Deleter>(pu,d)), pt(pu) {}
    
        template<class U>
        explicit shared_ptr(U* pu) :pa(new auximpl<U,default_deleter<U> >(pu,default_deleter<U>())), pt(pu) {}
    
        shared_ptr(const shared_ptr& s) :pa(s.pa), pt(s.pt) { inc(); }
    
        template<class U>
        shared_ptr(const shared_ptr<U>& s) :pa(s.pa), pt(s.pt) { inc(); }
    
        ~shared_ptr() { dec(); }
    
        shared_ptr& operator=(const shared_ptr& s)
        {
            if(this!=&s)
            {
                dec();
                pa = s.pa; pt=s.pt;
                inc();
            }        
            return *this;
        }
    
        T* operator->() const { return pt; }
        T& operator*() const { return *pt; }
    };
    

    Where weak_ptr interoperability is required a second counter (weak_count) is required in aux (will be incremented / decremented by weak_ptr), and delete pa must happen only when both the counters reach zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.