0

I need to implement a smart pointer in such a way that it does not allow more than 3 references.

my code looks like

template<typename T>
class smartptr{
smartptr():ptr(0), counter(new int){
            *counter = 0;
            (*counter)++;
        };
        ~smartptr()
        {
            (*counter)--;
            if(*counter == 0)
            {
                delete counter;
                delete ptr;
            }
        }

        T& GetData()
        {
            return *ptr;
        }

        T* GetPtr()
        {
            return ptr;
        }

        int GetCount()
        {
            return *counter;
        }

        smartptr(smartptr<T>& s):ptr(s.ptr), counter(s.counter)
        {
            if(*counter <= 3)
                (*counter)++;

            else
            {
                ptr = nullptr;
                counter = new int;
                *counter = 0;
            }
            
        }
        smartptr(T* p) : ptr(p), counter(new int)
        {
            *counter = 0;
            (*counter)++;
        }

        smartptr& operator=(smartptr &r)  // help!!!
        {
            if(*counter < 3)
            {
                ptr = r.ptr;
                (*counter)++;
            
            }
            else if(*counter == 3)
            {
                ptr = nullptr;
                counter = new int;
                *counter = 1;
            }

            else
            {
                delete ptr;
                counter = new int;
                *counter = 0;
                
            }
            
            return *this;

            
        }

    private:
        T* ptr;
        int* counter;

};

The problem I'm facing is in the copy assignment. If the reference count drops to 0, the referenced pointer's memory should be released. If the reference count is already 3 when copying, should set it's pointer to nullptr and reference count to 1. Thanks

jun
  • 142
  • 6
  • 1
    I see a number of problems with this code, not just with the copy assignment operator. – Remy Lebeau Jul 04 '20 at 00:26
  • 1
    [Rubber ducky](https://en.wikipedia.org/wiki/Rubber_duck_debugging) wants to know where in `counter` is updated to adopt the `counter` of the source `smartptr`. Ducky also wants to warn you about the leak because the object's current `counter` is not disposed of. – user4581301 Jul 04 '20 at 00:26
  • 1
    Obligatory link to [What is the copy-and-swap idiom?](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) It's not always the right choice but it's a great starting point because it is almost impossible to get wrong. – user4581301 Jul 04 '20 at 00:29
  • would appreciate some pinpoints in the code.. – jun Jul 04 '20 at 00:47
  • Your copy constructor will lead to a memory leak if called when `counter`is already equal to 3 since `count` is 0 but an int was allocated for it. – Phil1970 Jul 04 '20 at 03:31
  • While assignment operator have 3 cases, how is it possible that count would be greater than 3 *(last else clause)*? Having said that, the code in that case would lead to a leak of the counter. And since `ptr`is not nul, it could be dangerous of using a dandling reference. – Phil1970 Jul 04 '20 at 03:37
  • Having said that, it is not clear from the question, the problematic case, the expected result and the actual result. – Phil1970 Jul 04 '20 at 03:38

0 Answers0