Slides
Section : Smart Pointers
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Smart Pointers
3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Manually releasing memory yourself through the delete operator for raw
pointers is a pain in the neck. Smart pointers are a solution offered by modern
C++ to release the memory automatically when the pointer managing the
memory goes out of scope
4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
new
delete
5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Smart Pointers
new
delete
6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
<memory>
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• std::unique_ptr
• std::shared_ptr
• std::weak_ptr
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers
10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• At any given moment there can only be
one pointer managing the memory
• Memory is automatically released when the
pointer goes out of scope
11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
<memory>
12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Stack variables
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Raw pointers
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Std::make_unique
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Can’t copy unique pointers
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Moving ownership
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Resetting
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers as function
parameters & return values
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Passing by value
22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Passing by value
23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Passing by reference
24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Returning by value
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers and arrays
27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Array managed by unique_ptr
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
make_unique
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::unique_ptr : best practices
31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique_ptr member variables
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Using std::make_unique eliminates the last two problems. You’re not directly
dealing with the raw pointer, so you can’t easily misuse it. In modern C++, strive
to use smart pointers as much as possible and use new and delete directly only
if really necessary.
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared Pointers
37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
ptr_1
ptr_2
data
ptr_3
ptr_4
38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared pointers with fundamental types
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared pointers with fundamental types
40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared pointers with fundamental types
41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
make_shared
42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Creating shared pointers from
unique pointers
44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
shared_ptr to unique_ptr
46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Returning smart pointers
47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Having your functions return unique_ptr is the preferred way to do things, as you can
turn that pointer into a shared_ptr at any time, but you can’t turn a shared_ptr into a
unique_ptr. unique_ptr are much more flexible to work with in this case
48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared Pointers with arrays
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• make_shared syntax isn't supported yet for raw arrays. Some compilers do offer
some partial support for it, but I would not recommend using that in your code so I
won't show that here. If you find yourself needing to use shared_ptr with arrays, then
new is still your friend. But once the array is created, the shared_ptr is going to
manage the memory , you don't need to explicitly call delete.
• You won’t need to use raw arrays with smart pointers that often though, there are
better and more practical collection types we will learn about later in the course
that almost remove the need for raw arrays.
52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared Pointers as function
parameters and return value
54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
shared_ptr passed by value
55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
shared_ptr passed by non const ref
56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
shared_ptr passed by const ref
57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Returning by value
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Returning by Reference
NOT RECOMMENDED!
59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Weak pointers
61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Non owning pointers that don’t implement the -> or * operator. You can’t
use them directly to read or modify data
62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Using weak_ptr
63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Cyclic dependency problem
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Cyclic dependency problem
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
person_a person_b
Person(“Alison”) Person(“Beth”)
Friend
Friend
66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Solving cyclic dependency
67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Smart Pointers
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Manually releasing memory yourself through the delete operator for raw
pointers is a pain in the neck. Smart pointers are a solution offered by modern
C++ to release the memory automatically when the pointer managing the
memory goes out of scope
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• std::unique_ptr
• std::shared_ptr
• std::weak_ptr
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers
72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Std::make_unique
73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unique pointers as function
parameters & return values
75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Array managed by unique_ptr
76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
ptr_1
ptr_2
data
ptr_3
ptr_4
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared pointers with fundamental types
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
make_shared
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shared ptr from unique ptr
80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Solving cyclic dependency with weak pointers
81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya