Smart Pointer
Introduce to Smart Pointer
In computer science, a smart pointer is an abstract data type that simulate a pointer while providing additonal features. -- [WikiPedia](http://en.wikipedia.org/wiki/Smart_pointer)
In the C++ language, smart pointer implements as a template class and override the behaviour of raw pointer, such as dereferencing and assignment. What's more, smart poiner also providing addtional memory managment algorithms.
From the information above, we learn that:
- Smart pointer is a decorator of raw pointer. So smart pointer has more functions/methods than raw pointer, and use more convenient than raw pointer.
- Smart poiner is as well a template class, so it has the same usage of template class. For example: shared_ptr<int> int_ptr.
Two kind of smart pointers
- With shared pointer there could be multiple instance of the smart pointer pointing to the same object. It use some form of reference counting to manage the lifetime of the object they point to.
- With unique pointer, it has the restriction of only one instance of the smart pointer managing the object.
Difference between STL and Boost
Here are some saying from wiki and stackoverflow:
- std::shared_ptr is the C++0x form of tr1::shared_ptr, and boost's boost::shared_ptr should behave the same. -- wikiepedia
- According to the Wikipedia C++0x page: The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these. -- wikiepedia
- Now, C++11 provides?std::unique_ptr, as well as improvements to?std::shared_ptr?and?std::weak_ptr?from TR1.?std::auto_ptr?is deprecated. -- stackoverflow
In general, there is little difference between std and boost on smart pointer operations.
Performance on smart pointers
Define a class with a double variable, and just do one thing: variable num multiply itself 10,000 times.
The main function of raw pointer:
The main function of smart pointer:
Then let input to be 10,000:
Let input to be 50,000
Let input to be 100,000
Here is the graph of performance:
Conclusion
From the experienments has done, we come to the conclusion that smart pointer's performance is closed to raw pointer when using in the normal way.
Additional experiments
In order to get more details of smart pointer's performace, here is another experiment, just allocation but not calculate:
A new class:
The main function of smart pointer:
The main function of raw pointer:
And the result:
Conclusion: Compared to raw pointer, smart pointer spends more time on allocating.
Usages
First of all, #include <memory>
- One of constructors:
The key word explicit avoids compiler to construct object using implicit conversion. For example:
In the case without using explicit key word: obj will convert to shared_ptr type.
- create a smart pointer:
- create a vector of smart pointer:
Please notice we use "> >", not ">>". ">>" refers to stream operator.
- delete a smart pointer:
- Invoking a method looks like raw pointer.
- To get the raw pointer:
- To swap two smart pointers:
Reference
http://www.codesynthesis.com/~boris/blog/2010/05/24/smart-pointers-in-boost-tr1-cxx-x0/ http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds5%2Ftime.htm http://gcc.gnu.org/onlinedocs/gcc-4.6.0/libstdc++/api/a01033_source.html http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/smart_ptr.htm http://stackoverflow.com/questions/4902313/difference-between-boostshared-ptr-and-stdshared-ptr-from-the-standard-memo http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3225.pdf http://en.wikipedia.org/wiki/C%2B%2B0x#General-purpose_smart_pointers http://en.wikipedia.org/wiki/Smart_pointer#C.2B.2B_smart_pointers http://en.cppreference.com/w/cpp/memory/shared_ptr