咖啡香

Thinking in shared_ptr/auto_ptr

The father of C++ said that he never meet MLK issue after using auto pointer. It’s a good practice to use auto pointer to replace malloc/free. There are also some practice for auto pointer.

auto pointer in C++

Compared to C, constructor/deconstructor is an important improvement of C++. It’s not only make sure that the object/struct will be automatically initialized, but also keep C++ developer far away from MLK issue. The smart C++ developer used de-constructor to release memeory automatically when the reference holder destroy (out of scope, or delete). Here is an simple example to show how auto pointer work:

class SimpleAutoPointer
{
    public:
        SimpleAutoPointer(void* ptr)
        {
            m_rawPtr = ptr
        }

        ~SimpleAutoPointer()
        {
            delete m_rawPtr;
        }

    private:
        void* m_rawPtr;
};

int main(int argc, char** argv)
{
    {
        SimpleAutoPointer ptr(new int);
    } // the memory will be free when SimpleAutoPointer exit the scope
}

std::shared_ptr VS std::auto_ptr

The above sections of the code has lots of drawbacks:

  1. Can not delete the pointer that pointing to an array
  2. Assignment is invaild; if not reference counter, double free error will destroy your application
  3. Template is better than void*
  4. Can not access the raw pointer
  5. Can not execute raw pointer’s function
  6. ……

It’s really a hard work to build a strong auto pointer class; but STL has help us on completing this work. There are two kind of auto pointer: std::tr1::shared_ptr & std::tr1::auto_ptr.

auto pointer in practice

Thanks to STL’s great work, C++ developer has a strong auto pointer class to use; but there is still a trap which will introduce MLK issue, its name is “Circular References”. The “Circular References” will happen when the two objects point to each other by auto pointer. For now, there is no technical to resolve “Circular Reference” issue; but by following this coding practice, we can avoid this kind of issue: clarify the responsiblity of releasing the object; if the object did not have the responsiblity of releasing the object, it should hold the raw pointer; otherwise, it should hold the auto pointer. There must be reply that: how about the two object take the responsibility of each other? There are two options:

Summary

Enjoy your journey in C++ world with auto pointer!!!


comments powered by Disqus