Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Smart Pointer@v2

Introduction to Smart Pointer@v2

Introduction to Smart Pointer@v2
Present for cGeDBIT team

shonenada

May 30, 2013
Tweet

More Decks by shonenada

Other Decks in Technology

Transcript

  1. • More about Smart Pointer • C++0x VS Boost •

    Performance • Usage • Some advices • Reference Outline • More about Smart Pointer • C++0x VS Boost • Performance • Usage • Some advices • Reference
  2. • A decorator (wrapper) of raw pointer. • A template

    class. • Two Kinds of Smart Pointer: shared and unique More about Smart Pointer • A decorator (wrapper) of raw pointer. • A template class. • Two Kinds of Smart Pointer: shared and unique
  3. • A decorator (wrapper) of raw pointer. • A template

    class. • Two Kinds of Smart Pointer: shared and unique More about Smart Pointer • A decorator (wrapper) of raw pointer. • A template class. • Two Kinds of Smart Pointer: shared and unique
  4. • std::shared_ptr is the C++0x form of tr1::shared_ptr, and boost's

    boost::shared_ptr should behave the same. • 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. • 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. • In generally, there is little different between std and boost on smart pointer operations. C++0x VS Boost • std::shared_ptr is the C++0x form of tr1::shared_ptr, and boost's boost::shared_ptr should behave the same. • 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. • 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. • In generally, there is little different between std and boost on smart pointer operations.
  5. • Define a class with a double variable. • Just

    do one thing: variable num multiply itself 10,000 times. Performance • Define a class with a double variable. • Just do one thing: variable num multiply itself 10,000 times.
  6. Performance How about 10,000 times? How about 50,000 times? How

    about 50,000 times? How about 100,000 times?
  7. Performance • More tests for both raw pointer and smart

    pointer. • More tests for both raw pointer and smart pointer.
  8. Performance • What does the program do? • 1. Allocation.

    • 2. Multiplying. • 3.Freed. • !Important!: Each multiplying will multipy a double variable for 10,000 times. • What does the program do? • 1. Allocation. • 2. Multiplying. • 3.Freed. • !Important!: Each multiplying will multipy a double variable for 10,000 times.
  9. • In this test, the program actually does 10,000 *

    10,000 = 100,000,000 times multiplying. Performance • In this test, the program actually does 10,000 * 10,000 = 100,000,000 times multiplying.
  10. • In this test, the program actually does 50,000 *

    10,000 = 500,000,000 times multiplying. Performance • In this test, the program actually does 50,000 * 10,000 = 500,000,000 times multiplying.
  11. • In this test, the program actually does 100,000 *

    10,000 = 1,000,000,000 times multiplying. Performance • In this test, the program actually does 100,000 * 10,000 = 1,000,000,000 times multiplying.
  12. Usage • Create a shared pointer object • weak pointer

    • Delete • Invoke methods of class • Get the raw pointer • swap • Create a shared pointer object • weak pointer • Delete • Invoke methods of class • Get the raw pointer • swap
  13. Usage (Create) • One of constructors: The key word explicit

    avoids compiler to construct object using implicit conversion. For example: shared_ptr<MyClass> obj = new MyClass(); In the case without using explicit key word: obj will convert to shared_ptr type. More details in 《More Effective C++》
  14. • To create a smart pointer object, just: • Or

    take cGeDBIT as an example: • Vector: Usage (Create) • To create a smart pointer object, just: • Or take cGeDBIT as an example: • Vector: Be carefull!!
  15. • In the case like: • It is impossible to

    freed “Child1” and “Leaf” • So, weak_ptr appears Usage (weak pointer) Root • In the case like: • It is impossible to freed “Child1” and “Leaf” • So, weak_ptr appears Child1 Child2 Child3 Leaf
  16. • What is weak_ptr: • weak_ptr is a companion pointer

    which points to the object owned by shared_ptr without having an increment in the reference counter. Usage (weak pointer) • What is weak_ptr: • weak_ptr is a companion pointer which points to the object owned by shared_ptr without having an increment in the reference counter.
  17. • In another case like: • Child1 will be removed

    when Root is removed Usage (weak pointer) Root • In another case like: • Child1 will be removed when Root is removed Child1 Child2 Child3 Leaf
  18. Usage (delete) • To delete a shared_ptr object,use reset() method:

    • To delete a shared_ptr object,use reset() method:
  19. Some advices(Type Conversion) • Use C++-style type conversion • Use

    static_cast<newType*>(expression) instead of using (type*) (expression) • Safe & Readable • Use C++-style type conversion • Use static_cast<newType*>(expression) instead of using (type*) (expression) • Safe & Readable
  20. Some advices(more Type Conversion) • More C++-style type conversion •

    const_cast<type*>(expression) • dynamic_cast<type*>(expression)
  21. Some advices(Code Writing) • Codes are written by human. •

    Codes are read by human!!! • Tidy and readable is so important! • Codes are written by human. • Codes are read by human!!! • Tidy and readable is so important!
  22. • Varibale/Function Name • What is “a” Some advices(Code Writing)

    • Varibale/Function Name • What is “a”
  23. • Max length of a line 80 or 120 •

    My hands are on the keyboard, I don’t want to drag the scroll bar..... • https://github.com/facebook/folly/ • https://github.com/chenshuo/muduo-protorpc Some advices(Code Writing) • Max length of a line 80 or 120 • My hands are on the keyboard, I don’t want to drag the scroll bar..... • https://github.com/facebook/folly/ • https://github.com/chenshuo/muduo-protorpc
  24. • More space and More Enter • Space on both

    side of operator. Some advices(Code Writing) • More space and More Enter • Space on both side of operator.
  25. • NO Zombie code, VCS can help you! Some advices(Code

    Writing) • NO Zombie code, VCS can help you!
  26. • Don’t #include, unless you need them. More advices(Code Style)

    • Don’t #include, unless you need them.
  27. • The order of #include s. • 1、class.h • 2、C

    system files. • 3、C++ system files. • 4、Other libraries’s .h files • 5、Your project’s .h files More advices(Code Style) • The order of #include s. • 1、class.h • 2、C system files. • 3、C++ system files. • 4、Other libraries’s .h files • 5、Your project’s .h files
  28. • shorten your function / method. • Normally,don’t longer than

    40 lines. More advices(Code Style) • shorten your function / method. • Normally,don’t longer than 40 lines.
  29. • 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%2F doc%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 • http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml • 《More Effective C++》 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%2F doc%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 • http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml • 《More Effective C++》