• 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
Slide 3
Slide 3 text
More about Smart Pointer
More about Smart Pointer
Slide 4
Slide 4 text
• 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
Slide 5
Slide 5 text
• 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
Slide 6
Slide 6 text
C++0x VS Boost
C++0x VS Boost
Slide 7
Slide 7 text
• 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.
Slide 8
Slide 8 text
Performance
Performance
Slide 9
Slide 9 text
• 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.
Slide 10
Slide 10 text
Performance
Raw Pointer Smart Pointer
Slide 11
Slide 11 text
Performance
• Simple test for
Raw Pointer.
• Simple test for
Raw Pointer.
Slide 12
Slide 12 text
• Simple test for
Smart Pointer.
Performance
• Simple test for
Smart Pointer.
Slide 13
Slide 13 text
Performance
Slide 14
Slide 14 text
Performance
How about 10,000 times?
How about 50,000 times?
How about 50,000 times?
How about 100,000 times?
Slide 15
Slide 15 text
Performance
• More tests for
both
raw pointer
and
smart pointer.
• More tests for
both
raw pointer
and
smart pointer.
Slide 16
Slide 16 text
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.
Slide 17
Slide 17 text
• 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.
Slide 18
Slide 18 text
• 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.
Slide 19
Slide 19 text
• 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.
Slide 20
Slide 20 text
Performance
Slide 21
Slide 21 text
Performance
Slide 22
Slide 22 text
• Just allocation:
Performance
Slide 23
Slide 23 text
• Compared to raw pointer, smart pointer
spends more time on allocating.
Performance
Slide 24
Slide 24 text
Usage
Usage
Slide 25
Slide 25 text
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
Slide 26
Slide 26 text
#include
#include
Slide 27
Slide 27 text
Usage (Create)
• One of constructors:
The key word explicit avoids compiler to construct object using implicit
conversion.
For example:
shared_ptr obj = new MyClass();
In the case without using explicit key word:
obj will convert to shared_ptr type.
More details in 《More Effective C++》
Slide 28
Slide 28 text
• 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!!
Slide 29
Slide 29 text
• 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
Slide 30
Slide 30 text
• 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.
Slide 31
Slide 31 text
• 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
Slide 32
Slide 32 text
Usage (weak pointer)
Slide 33
Slide 33 text
Usage (delete)
• To delete a shared_ptr object,use
reset() method:
• To delete a shared_ptr object,use
reset() method:
Slide 34
Slide 34 text
• Invoking a method looks like raw pointer.
Usage (Invoke)
Slide 35
Slide 35 text
• To get the raw pointer, use get() method:
Usage (Get raw poiner)
Slide 36
Slide 36 text
• Also take cGeDBIT as an example:
Usage(Get raw poiner)
Slide 37
Slide 37 text
Usage(swap)
Slide 38
Slide 38 text
Some advices
Some advices
Slide 39
Slide 39 text
Some advices
• Type Conversion
• Code Writing.
Slide 40
Slide 40 text
Some advices(Type Conversion)
• Use C++-style type conversion
• Use static_cast(expression) instead of
using (type*) (expression)
• Safe & Readable
• Use C++-style type conversion
• Use static_cast(expression) instead of
using (type*) (expression)
• Safe & Readable
Slide 41
Slide 41 text
Some advices(more Type Conversion)
• More C++-style type conversion
• const_cast(expression)
• dynamic_cast(expression)
Slide 42
Slide 42 text
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!
Slide 43
Slide 43 text
• Varibale/Function Name
• What is “a”
Some advices(Code Writing)
• Varibale/Function Name
• What is “a”
Slide 44
Slide 44 text
• Max length of a line
• 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
• 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
Slide 45
Slide 45 text
• 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.
Slide 46
Slide 46 text
• 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.
Slide 47
Slide 47 text
• Suitable brackets
Some advices(Code Writing)
• Suitable brackets
Slide 48
Slide 48 text
• NO Zombie code, VCS can help you!
Some advices(Code Writing)
• NO Zombie code, VCS can help you!