1
00:00:00,240 --> 00:00:05,580
OK, everybody, so this is going to be an extra lecture here on smart pointers.

2
00:00:06,450 --> 00:00:11,550
In the last lecture, we went over a smart pointer called Unique Pointer, but there was some feedback

3
00:00:11,550 --> 00:00:15,400
on the course that people really want to know about the other smart pointers.

4
00:00:15,420 --> 00:00:20,010
I wasn't going to include them because I thought it was just something more specific to learning C++

5
00:00:20,010 --> 00:00:20,460
itself.

6
00:00:21,800 --> 00:00:23,300
Less of a general programming thing.

7
00:00:23,930 --> 00:00:29,570
But I will go ahead and go over it because it seems like, you know, it might be useful and there's

8
00:00:29,570 --> 00:00:33,890
interest in it, so I'm going to go over to other pointers, one called shared pointer and one called

9
00:00:33,890 --> 00:00:34,460
weak pointers.

10
00:00:34,460 --> 00:00:36,950
So those are the two other pointers.

11
00:00:36,950 --> 00:00:40,790
There's three in total unique shared and weak smart pointers.

12
00:00:40,790 --> 00:00:42,920
So I've made a little program here.

13
00:00:43,040 --> 00:00:47,300
It's called Speed CBP and I've put some stuff in here.

14
00:00:47,300 --> 00:00:49,610
We have a class that's just called something.

15
00:00:49,610 --> 00:00:58,220
You notice that it has a public constructor and destructor, as well as a little method or member function

16
00:00:58,220 --> 00:01:02,420
here called Get Thing, which just prints out this one private data member, right?

17
00:01:02,960 --> 00:01:11,900
So what we've done so far is just make a unique pointer like this right and type something, and we

18
00:01:11,900 --> 00:01:15,860
can just call this, you know, like you P1 and we've done something like this.

19
00:01:18,320 --> 00:01:21,330
So this will just make a unique pointer, right?

20
00:01:21,380 --> 00:01:25,220
And if I go, what I can do

21
00:01:28,970 --> 00:01:32,480
is like, let's say I make two of these right like you P1 and you P2.

22
00:01:32,480 --> 00:01:37,730
And what we talked about was when this when these pointers go out of scope, which is here in the function,

23
00:01:37,730 --> 00:01:37,940
right?

24
00:01:37,940 --> 00:01:40,910
Once it gets past this bracket returns zero and goes past year.

25
00:01:41,180 --> 00:01:46,100
That's when these two go out of scope and they automatically will d allocate themselves.

26
00:01:46,430 --> 00:01:48,560
They will automatically call the destructor right.

27
00:01:49,010 --> 00:01:52,160
And here is the destructor, and it prints out the allocated something.

28
00:01:52,160 --> 00:01:54,170
So we should see this printout twice, right?

29
00:01:54,770 --> 00:02:02,060
So if I go back here, I compile this and it's just going to be out, I noticed this SD allocated something

30
00:02:02,060 --> 00:02:03,020
twice, right?

31
00:02:04,490 --> 00:02:07,250
So that's what we've been doing so far.

32
00:02:07,730 --> 00:02:16,110
So another thing that I'd like to point out is that there is another way to make these pointers first.

33
00:02:16,130 --> 00:02:20,960
So what you can actually do is you don't have to use this syntax right here.

34
00:02:21,530 --> 00:02:27,920
What you can instead do is like you P2 equals and then this is a special kind of constructor constructor

35
00:02:27,920 --> 00:02:28,190
here.

36
00:02:28,730 --> 00:02:36,400
It's actually called make unique and you just say type something right here and you can put some little

37
00:02:36,410 --> 00:02:37,490
parentheses like that.

38
00:02:37,880 --> 00:02:40,280
This is kind of a preferred way to do it.

39
00:02:41,510 --> 00:02:42,680
These will both work.

40
00:02:42,800 --> 00:02:52,520
The only thing is that when you use this special kind of way of allocating the memory for a unique pointer,

41
00:02:52,520 --> 00:02:58,690
it actually uses something out of this code base here, and it can deal with exceptions.

42
00:02:58,700 --> 00:03:01,580
So we've in this course, we talk a little bit about exceptions.

43
00:03:02,330 --> 00:03:07,940
If you do it like this, you may run into some issues if you are dealing, with some exceptions to do

44
00:03:07,940 --> 00:03:08,480
with the pointer.

45
00:03:08,750 --> 00:03:15,410
So this is kind of known as a better syntax to use to make it, although this is totally acceptable

46
00:03:15,410 --> 00:03:17,060
most of the time too as well.

47
00:03:18,140 --> 00:03:22,430
So I just want to point that out, and we will be needing to use this style of syntax for the other

48
00:03:23,030 --> 00:03:23,660
smart pointer.

49
00:03:23,670 --> 00:03:27,250
So let's get into the next type of smart pointer.

50
00:03:27,260 --> 00:03:32,210
So the thing about the unique pointer that we mainly talked about is it just goes out of scope and it's

51
00:03:32,210 --> 00:03:33,260
basically done right.

52
00:03:33,260 --> 00:03:35,390
It makes a new piece of memory.

53
00:03:35,750 --> 00:03:38,840
And then once it goes out of scope, it's totally done.

54
00:03:38,850 --> 00:03:43,210
So a question that would lead us to talk about the next type of smart pointers.

55
00:03:43,220 --> 00:03:45,260
What if we wanted to do something like this?

56
00:03:45,980 --> 00:03:47,920
We just wanted to make another pointer, right?

57
00:03:48,260 --> 00:03:53,720
Well, like, hey, we have a smart pointer that's pointing to, you know, up one right?

58
00:03:53,720 --> 00:03:55,510
Or it's pointing to a new something.

59
00:03:55,670 --> 00:03:56,600
Let's call up one.

60
00:03:56,600 --> 00:03:58,670
And what if I just want to make another pointer, right?

61
00:03:59,540 --> 00:04:04,940
Well, this is pointing to the same piece of memory once this goes out of scope, basically, you know,

62
00:04:04,940 --> 00:04:07,370
this one is automatically deleted.

63
00:04:07,940 --> 00:04:10,430
So and you don't actually, this is going to be a problem.

64
00:04:10,430 --> 00:04:14,900
You won't be able to actually do this even right here because it's not really meant for this type of

65
00:04:14,900 --> 00:04:15,200
thing.

66
00:04:15,200 --> 00:04:20,210
It's only meant to be like a unique standalone point of view, hence why it's called unique pointer.

67
00:04:20,220 --> 00:04:22,130
So what if I try and compile this right here?

68
00:04:24,110 --> 00:04:29,420
So it actually says no, it has an error and it says like use of deleted function.

69
00:04:29,630 --> 00:04:34,730
And that's because if you look at the code base of unique pointer where it's written and implemented,

70
00:04:34,730 --> 00:04:39,710
it actually does, you know, just delete it so you can't actually use this anymore.

71
00:04:39,740 --> 00:04:40,580
It's just done.

72
00:04:40,940 --> 00:04:45,170
So you cannot make a copy with the assignment operator right here.

73
00:04:46,630 --> 00:04:47,140
So.

74
00:04:48,400 --> 00:04:53,230
What do we do if we want to do something like this, we are not going to use a unique pointer, but

75
00:04:53,230 --> 00:04:57,730
what we are going to use is something called a shared a pointer because we're going to share the same

76
00:04:57,730 --> 00:04:59,140
region of memory, right?

77
00:04:59,950 --> 00:05:04,250
So we can make a shared pointer call up to equals update one.

78
00:05:04,270 --> 00:05:11,650
So what we really should do is instead, I make like a shared pointer right here.

79
00:05:11,650 --> 00:05:20,250
But what I'm going to do is I'm going to instead make make it using that different style, right?

80
00:05:20,650 --> 00:05:22,810
That different style of allocating.

81
00:05:23,050 --> 00:05:31,300
So I'm going to say, firstly, make shared instead of make unique and I'm going to say something just

82
00:05:31,300 --> 00:05:31,840
like that.

83
00:05:32,410 --> 00:05:37,330
And what I can do is I'll just say ESP two equals ESP one.

84
00:05:37,330 --> 00:05:42,640
So this is the type of thing I would want to do if I want to make like another pointer pointing to the

85
00:05:42,640 --> 00:05:44,020
same piece of memory.

86
00:05:45,030 --> 00:05:50,490
And this is how I would want to initialize it in the beginning, right, if I make the initial shared

87
00:05:50,730 --> 00:05:58,920
or one, I would want to use this special part of coming from the memory code base there and say, make

88
00:05:58,920 --> 00:05:59,370
shared.

89
00:06:00,030 --> 00:06:03,960
So what's cool about this, though?

90
00:06:03,960 --> 00:06:05,320
Like what's the why would we use this?

91
00:06:05,340 --> 00:06:11,940
Well, of course we talked about this, but another really cool thing is that once this shared pointer

92
00:06:12,120 --> 00:06:14,550
SP1 one goes out of scope.

93
00:06:15,500 --> 00:06:19,310
It doesn't necessarily allocate the object.

94
00:06:20,680 --> 00:06:20,990
Right.

95
00:06:21,020 --> 00:06:25,420
So let me just let me show you kind of like what's going on here.

96
00:06:25,420 --> 00:06:31,740
So this only makes one piece of memory, right?

97
00:06:31,750 --> 00:06:34,510
So if I go ahead and run this right now?

98
00:06:36,240 --> 00:06:37,590
Compile it and run it.

99
00:06:38,580 --> 00:06:40,710
It only says the allocated something once.

100
00:06:41,220 --> 00:06:44,800
And that's because this is the only real allocated memory.

101
00:06:44,820 --> 00:06:48,180
This is just another pointer pointing to the same memory.

102
00:06:48,600 --> 00:06:56,580
The really cool thing, though, is that once SP1 goes out of scope, it doesn't delete it right then.

103
00:06:58,410 --> 00:06:58,740
Right.

104
00:06:59,190 --> 00:07:04,080
So if I weren't right here, I'm just going to put some sort of like, you know, scope block.

105
00:07:04,800 --> 00:07:16,890
So if I put something in here, let's just say I make another thing here, so we'll have a shared pointer

106
00:07:17,040 --> 00:07:18,320
SP1 here.

107
00:07:18,330 --> 00:07:21,690
Actually, I'm going to put this in here.

108
00:07:23,190 --> 00:07:25,500
I'm going to have another thing up here.

109
00:07:25,800 --> 00:07:32,340
So if I say shared pointer something and I'll just call this like s

110
00:07:36,090 --> 00:07:43,680
and what I will do is, I will say s equals one.

111
00:07:47,500 --> 00:07:51,940
Right here, so I'm declaring one up here, right, and then I make one here and I'm pointing this,

112
00:07:52,420 --> 00:07:56,080
I'm basically copying like, I'm making another pointer.

113
00:07:56,080 --> 00:08:00,070
This and this is right here is pointing to the same region of memory as this right here.

114
00:08:00,070 --> 00:08:01,330
This spun right.

115
00:08:01,690 --> 00:08:03,280
So shared another shared pointer.

116
00:08:05,010 --> 00:08:12,330
This shared pointer, they all share this one like control block is what it is like a one piece of memory,

117
00:08:12,340 --> 00:08:12,600
right?

118
00:08:12,600 --> 00:08:17,280
And it keeps track of how many pointers you have pointing to that memory.

119
00:08:17,550 --> 00:08:21,030
It's like special in that sense that it keeps track of all the references.

120
00:08:21,030 --> 00:08:22,110
It has a reference count.

121
00:08:22,110 --> 00:08:25,710
How many pointers are referring to this one control block of memory?

122
00:08:26,820 --> 00:08:29,130
In this case, there's two, right.

123
00:08:29,130 --> 00:08:34,020
We have one right here pointing to it, and then we have this one pointing to the same block of memory,

124
00:08:34,020 --> 00:08:34,290
right?

125
00:08:35,250 --> 00:08:40,410
This is the really cool thing is that this was created in this scope of this.

126
00:08:40,410 --> 00:08:46,320
If, right, once this goes out of scope, you would think if it was like unique pointer, it would

127
00:08:46,320 --> 00:08:50,070
delete it and we would no longer have access to that region of memory.

128
00:08:50,070 --> 00:08:57,090
So we wouldn't be able to do something, for example, like s get thing right.

129
00:08:58,590 --> 00:09:01,530
So what if I wanted to, you know, that's going to print out the thing.

130
00:09:01,530 --> 00:09:02,070
So.

131
00:09:04,140 --> 00:09:07,470
You would think that I wouldn't be able to do this right because this memory would be gone.

132
00:09:08,700 --> 00:09:10,530
Well, it's actually totally fine.

133
00:09:11,190 --> 00:09:15,030
So I'll show you it'll go ahead and print it out.

134
00:09:15,600 --> 00:09:20,880
So if I compile this and I run it, you know, there was no compiler errors there and noticed that it

135
00:09:20,880 --> 00:09:23,260
printed it out and then it dislocated it, right?

136
00:09:23,280 --> 00:09:30,960
And so what it was really doing is it waited until this one went out of scope, right?

137
00:09:30,980 --> 00:09:34,190
This is the last reference pointing to that region of memory.

138
00:09:34,200 --> 00:09:37,230
So it waited for us to go out of scope here.

139
00:09:37,560 --> 00:09:42,510
That's why we saw this thing is zero originally right?

140
00:09:42,510 --> 00:09:43,710
And it printed out here.

141
00:09:43,710 --> 00:09:48,420
So we saw it print out here before the allocated it right was the allocated.

142
00:09:48,420 --> 00:09:49,890
Something happened after that.

143
00:09:51,270 --> 00:09:56,010
You know, if we go here, you notice that zero printed out and then reallocated something printed out.

144
00:09:57,120 --> 00:10:01,830
So that means that the SE did not get the allocated until down here.

145
00:10:02,010 --> 00:10:07,860
So what it does is that the shared pointer keeps track of all of the references and the reference count,

146
00:10:08,310 --> 00:10:13,080
and it's not going to delete the region of memory.

147
00:10:13,080 --> 00:10:19,350
It's not going to allocate it until all of the reference cat like the very last pointer pointing to

148
00:10:19,350 --> 00:10:20,580
it goes out of scope.

149
00:10:20,760 --> 00:10:22,470
That's when it's going to be allocated.

150
00:10:22,860 --> 00:10:24,300
So that's what's cool about the SharePoint.

151
00:10:24,420 --> 00:10:29,310
Like, we can make a lot of things pointing to the same region of memory, and we don't have to like,

152
00:10:29,310 --> 00:10:36,150
rely on being in the same scope and having that memory like it deleted, you know, like a unique pointer

153
00:10:36,150 --> 00:10:37,640
would have totally gone out of there.

154
00:10:37,650 --> 00:10:42,390
The problem is we can't we weren't even able to assign, you know, another thing to the unique pointer.

155
00:10:43,540 --> 00:10:47,980
So we couldn't even test that out if I wanted to do the same thing with unique pointer because I cannot

156
00:10:47,980 --> 00:10:53,200
like make something else pointing to the same region of memory since it's unique.

157
00:10:54,160 --> 00:11:00,640
So that is a really cool thing about the shared pointer, and I can make as many references as I want

158
00:11:00,640 --> 00:11:06,910
to to this one region of memory that was originally, you know, SP1 was pointing to it originally.

159
00:11:07,120 --> 00:11:10,900
I can make a bunch more shared pointers pointing to the same thing, and they could all be at their

160
00:11:10,900 --> 00:11:12,160
own levels of scope.

161
00:11:12,610 --> 00:11:21,610
And I wouldn't have to worry about the, you know, memory going being allocated before these other

162
00:11:21,610 --> 00:11:24,320
pointers that I made going or going out of scope.

163
00:11:24,350 --> 00:11:26,200
You know, if I want to still access them.

164
00:11:27,700 --> 00:11:35,290
So there is one more type of pointer and that is a weak pointer.

165
00:11:35,590 --> 00:11:44,020
So the weak pointer is similar to the shared pointer, except it doesn't add another reference count

166
00:11:44,110 --> 00:11:49,660
like noticed that this thing added another reference count to the shared pointer, right?

167
00:11:49,710 --> 00:11:52,870
It has this control block, and it's keeping track of all the references.

168
00:11:53,110 --> 00:11:54,490
How many references are there?

169
00:11:54,850 --> 00:11:59,650
A weak pointer would be something that I guess you would only want to use temporarily because it wouldn't

170
00:11:59,650 --> 00:12:04,240
be considered, as you know, an extra reference count.

171
00:12:04,240 --> 00:12:16,140
So if I made this like a weak pointer, so I make this weak point s, then it's not going to be considered

172
00:12:16,150 --> 00:12:16,870
reference count.

173
00:12:16,900 --> 00:12:20,380
So when this goes out of scope, it'll totally be out of scope.
