1
00:00:01,840 --> 00:00:02,440
OK.

2
00:00:02,500 --> 00:00:03,770
How's it going, everybody?

3
00:00:03,790 --> 00:00:12,550
So we are coming back to a little bit of what we talked about in the last slideshow lecture, we went

4
00:00:12,550 --> 00:00:20,260
over quite a bit of what may have seemed like a complex topic, so we were looking into the virtual

5
00:00:20,590 --> 00:00:22,270
process memory.

6
00:00:22,450 --> 00:00:25,990
So we talked a little bit about that in general.

7
00:00:25,990 --> 00:00:31,350
And then we were talking about those pointers and dynamic memory allocation.

8
00:00:31,360 --> 00:00:35,620
And of course, she didn't think that I was going to leave you hanging with that.

9
00:00:36,160 --> 00:00:37,780
That's kind of a confusing topic.

10
00:00:37,780 --> 00:00:45,220
So don't worry, we are going to slowly familiarize ourself with that stuff, and I'm going to make

11
00:00:45,220 --> 00:00:55,330
a few lectures where we go into this ED and we actually look into dynamic allocated dynamically allocated

12
00:00:55,330 --> 00:01:00,790
memory and pointers in general, whether they be on the stack or the heap and just kind of get some

13
00:01:00,790 --> 00:01:01,330
practice.

14
00:01:01,330 --> 00:01:02,950
So you all feel comfortable with it.

15
00:01:03,820 --> 00:01:05,290
So that's what we're doing in this lecture.

16
00:01:05,980 --> 00:01:14,020
I'm going to go ahead and make a few pointers, both on the stack and on the heap, so they will be

17
00:01:14,020 --> 00:01:17,530
dynamically allocated ones and not dynamically allocated ones.

18
00:01:19,670 --> 00:01:23,690
Like, I don't mean that the pointers are being dynamically allocated, I just mean, we'll make some

19
00:01:23,690 --> 00:01:29,900
dynamically allocated memory and we'll of course, use pointers to point to that and then we'll have

20
00:01:29,900 --> 00:01:31,520
some pointers to just point to things.

21
00:01:31,520 --> 00:01:38,930
But that's, you know, the variables that we're trying to have point to will be on the stack.

22
00:01:40,010 --> 00:01:46,700
So let's go ahead and mess around with some of that, so I'm going to go ahead and just use integers

23
00:01:46,700 --> 00:01:47,600
to start out with.

24
00:01:48,710 --> 00:01:57,200
So what I'm going to do is I'm just going to say and and then a little star for the pointer like we

25
00:01:57,200 --> 00:01:58,040
were talking about.

26
00:01:58,490 --> 00:02:00,470
This is the multiplication symbol, right?

27
00:02:00,500 --> 00:02:03,440
But when I use it like, like this?

28
00:02:05,340 --> 00:02:12,720
Im starting to make a new pointer variable here that is most likely.

29
00:02:13,260 --> 00:02:16,350
We're saying that it's going to be pointing to an integer.

30
00:02:17,350 --> 00:02:19,900
But this part right here is what's important.

31
00:02:20,020 --> 00:02:25,570
So this is actually saying that this is going to be a pointer and then here we have the variable name

32
00:02:26,320 --> 00:02:27,190
of the pointer.

33
00:02:28,750 --> 00:02:31,480
So I just declare this pointer right?

34
00:02:32,770 --> 00:02:44,230
And then I'm going to make another point here, but I'm going to call this my dynamic pointer and I'm

35
00:02:44,230 --> 00:02:48,540
going to actually say equals new end.

36
00:02:49,890 --> 00:02:52,840
And so remember to back to the last lecture.

37
00:02:53,330 --> 00:03:00,820
The thing that makes it dynamically allocated is the fact that we're using this new keyword right here.

38
00:03:02,400 --> 00:03:06,340
So we've done this pretty much the same thing on this side of the equal sign.

39
00:03:06,360 --> 00:03:14,550
We're just making a variable that's a pointer, but then over here we're saying, OK, set aside some

40
00:03:14,550 --> 00:03:21,210
space on the heap so we can have some dynamically allocated memory that we can point to now.

41
00:03:23,700 --> 00:03:27,720
And we're saying that that's going to be an integer type, integer day to day.

42
00:03:30,000 --> 00:03:39,390
So what I want to do now, as before we do anything else, I want to show you what the pointer is and

43
00:03:39,390 --> 00:03:43,080
what the pointer is pointing to at this point in time.

44
00:03:44,190 --> 00:03:44,880
So many points.

45
00:03:45,330 --> 00:03:50,760
So I am going to simply point out my pointer.

46
00:03:53,360 --> 00:04:01,490
And what I what I'll do is I put a little label here, so I'll just say my pointer equals and then I'll

47
00:04:01,490 --> 00:04:08,660
just say my pointer, and then I'm also going to say, what do you want from my dynamic corner?

48
00:04:08,660 --> 00:04:15,350
So I say my dynamic pointer equals and then I'll say my dynamic pointer.

49
00:04:18,920 --> 00:04:21,620
And then we will say, actually, you know what?

50
00:04:22,310 --> 00:04:25,880
I put this in between because I'd like to couple the variables together.

51
00:04:26,480 --> 00:04:40,640
So I'll say my pointer value equals and then what we will do is we will use the star again with my pointer.

52
00:04:40,640 --> 00:04:47,630
Accept what we are doing now is we are d referencing the pointer, which just means that we are accessing

53
00:04:47,630 --> 00:04:52,400
the thing that the pointer is pointing to, which is our integer right?

54
00:04:54,070 --> 00:05:01,720
Except we have not initialized this variable yet, we've only declared it, so that's why I wanted to

55
00:05:01,720 --> 00:05:06,880
print this out to kind of show you that there will be a garbage value stored in here.

56
00:05:09,790 --> 00:05:11,910
So I go ahead and I'm going to print that out.

57
00:05:11,920 --> 00:05:17,920
I'm going to do the exact same thing for this, except I'm just going to copy paste it down here.

58
00:05:18,190 --> 00:05:18,670
So

59
00:05:21,490 --> 00:05:27,400
we'll do this, but we'll say my dynamic pointer value equals and then we'll just reference my dynamic

60
00:05:27,400 --> 00:05:28,480
pointer as well.

61
00:05:28,480 --> 00:05:33,290
So syntactically you noticed that this all this stuff is very similar.

62
00:05:33,310 --> 00:05:40,000
The only difference was when we initially allocated the space for this variable in the heap.

63
00:05:40,000 --> 00:05:43,990
For our dynamically allocated one, we had to use this new and.

64
00:05:45,490 --> 00:05:52,840
Right, but everything is subsequent is pretty much the same, we did reference both of them the same

65
00:05:52,840 --> 00:05:55,150
way by putting the asterisk in front of it.

66
00:05:57,230 --> 00:05:59,510
Right, so we're printing this out now.

67
00:06:00,500 --> 00:06:06,260
I'm going to go ahead and just save that program right now and then run it just so we can see what we're

68
00:06:06,260 --> 00:06:07,370
dealing with so far.

69
00:06:08,420 --> 00:06:12,350
So I'm going to go ahead and compile this and run it.

70
00:06:13,820 --> 00:06:14,120
All right.

71
00:06:14,120 --> 00:06:16,340
So let's check out what what we have here.

72
00:06:16,610 --> 00:06:19,850
So I'm going to make some more space here in the console.

73
00:06:20,650 --> 00:06:23,690
Hopefully, I can scroll up without going too far.

74
00:06:23,720 --> 00:06:26,890
OK, so look what we see here.

75
00:06:26,900 --> 00:06:31,550
It says my pointer equals and then we have this hex number.

76
00:06:31,560 --> 00:06:34,820
So this is the hex address I was talking about.

77
00:06:35,720 --> 00:06:40,370
Remember, we were talking about all those parking spots in memory or all of those houses, whatever

78
00:06:40,370 --> 00:06:43,520
the analogy that fit best for you.

79
00:06:44,460 --> 00:06:49,980
That's what we're looking at now, we're looking at the address because remember, the pointer itself

80
00:06:49,980 --> 00:06:54,870
is just a number, it's just holding an address.

81
00:06:55,620 --> 00:06:56,040
Right?

82
00:06:56,610 --> 00:07:00,060
But there's something stored at that address that we care about.

83
00:07:00,060 --> 00:07:04,530
And so when we're pointing to something, it just has the address.

84
00:07:04,530 --> 00:07:08,790
But when we do reference it, look down here, it says my pointer value.

85
00:07:09,750 --> 00:07:14,520
Here, when we put the asterisk in front of it, when we're printed it out, we're saying, OK, I don't

86
00:07:14,520 --> 00:07:19,350
want the address, I want you to now use that address.

87
00:07:19,450 --> 00:07:23,280
Go to that address and show me what is stored at that address.

88
00:07:24,410 --> 00:07:30,110
So that is the difference here, we're getting the actual value that stored at the address here, when

89
00:07:30,110 --> 00:07:34,310
we print out the pointer itself, we noticed that the pointer is nothing but a no.

90
00:07:34,310 --> 00:07:35,570
It's just an address, right?

91
00:07:36,470 --> 00:07:38,360
So a very important difference.

92
00:07:38,810 --> 00:07:43,100
A lot of people are confused by foreigners who don't really understand what they are, especially when

93
00:07:43,100 --> 00:07:47,810
they see data types in front of the point there, like an integer pulling a string pointer, a flow

94
00:07:47,820 --> 00:07:48,260
pointer.

95
00:07:48,560 --> 00:07:50,210
These are all different things.

96
00:07:50,540 --> 00:07:51,970
They're not different things.

97
00:07:53,000 --> 00:07:59,390
All, no matter what data type you put in front of the pointer, it is not any different than any of

98
00:07:59,390 --> 00:08:02,540
the other data types because technically it's just storing an address.

99
00:08:02,540 --> 00:08:04,970
The pointer is always just going to be an address.

100
00:08:06,560 --> 00:08:12,470
The thing that is stored at that address might be a different thing and different data type.

101
00:08:12,800 --> 00:08:16,850
But the pointer itself is always going to be this highlighted thing right here.

102
00:08:16,850 --> 00:08:22,910
It's going to be some sort of address and the default way that you're going to view these addresses

103
00:08:22,910 --> 00:08:26,270
is in hexadecimal format, right?

104
00:08:28,700 --> 00:08:30,650
So pretty cool.

105
00:08:31,670 --> 00:08:35,050
Let's go ahead and continue on instead of having these values.

106
00:08:35,060 --> 00:08:36,740
Yeah, I should probably talk a little bit about that.

107
00:08:37,730 --> 00:08:39,530
There's some huge numbers in here, right?

108
00:08:40,280 --> 00:08:41,960
And we've seen this before.

109
00:08:42,860 --> 00:08:46,220
This is, you know, these things can totally change.

110
00:08:47,060 --> 00:08:50,510
Like, let's say I compile this again, I run this again.

111
00:08:53,080 --> 00:08:55,180
Let's see, we might have the same ones, actually.

112
00:08:56,360 --> 00:08:58,610
Dynamic point or no, it's actually.

113
00:08:58,680 --> 00:09:03,860
So the first one is the same, but the second one is different.

114
00:09:04,400 --> 00:09:04,700
Right?

115
00:09:05,270 --> 00:09:10,580
So look at the dynamic pointer value here and look at the dynamic pointer value here.

116
00:09:10,850 --> 00:09:13,100
We have a different number here.

117
00:09:13,130 --> 00:09:14,990
When I compile it and ran it again.

118
00:09:15,020 --> 00:09:17,870
So these are just random garbage values.

119
00:09:18,210 --> 00:09:20,170
They're being, you know.

120
00:09:21,470 --> 00:09:26,930
It's not really I don't want to say, stored so far, but we're just the things that are at these addresses

121
00:09:26,930 --> 00:09:31,250
and memory are just whatever, whatever was there.

122
00:09:31,550 --> 00:09:33,290
You know, and we we don't really know what's there.

123
00:09:33,310 --> 00:09:37,610
It's just kind of a mystery because we haven't initialized these to what we want to yet.

124
00:09:38,150 --> 00:09:41,450
So some random number is just sitting in there.

125
00:09:41,450 --> 00:09:47,600
We just have random data hanging out in these un initialized variables, right?

126
00:09:47,600 --> 00:09:50,110
And I keep referring to these variables.

127
00:09:50,120 --> 00:09:55,340
Remember, these are the pointers, but the things that they're pointing to, the things that lie at

128
00:09:55,340 --> 00:09:57,800
these addresses, for example.

129
00:09:59,370 --> 00:10:05,010
This address, if you go to this highlighted address, this is stored there.

130
00:10:05,340 --> 00:10:10,830
The first time we run it, when we went to this highlighted address notice, it's another address.

131
00:10:11,700 --> 00:10:14,100
It's not necessarily going to be the same every time.

132
00:10:15,240 --> 00:10:17,460
You know, this is just how the operating system works.

133
00:10:17,460 --> 00:10:20,430
It's just doing its thing, making these addresses.

134
00:10:21,490 --> 00:10:26,680
And you noticed there was a different value stored there, so we're just going to get these different

135
00:10:26,680 --> 00:10:34,690
random garbage values until we actually specifically reference these addresses and put something in

136
00:10:34,690 --> 00:10:35,920
there or store something in there.

137
00:10:35,920 --> 00:10:37,360
So let's go ahead and do that right now.

138
00:10:38,830 --> 00:10:45,250
So now I'm going to just d reference my my point here.

139
00:10:46,780 --> 00:10:49,280
So this is the one that's just on the stack, right?

140
00:10:49,300 --> 00:10:54,970
This is not dynamically allocated, you know, we're in the main function, so it's going to be on the

141
00:10:54,970 --> 00:10:58,480
stack for the main function.

142
00:10:59,410 --> 00:11:01,930
So we're going to now store something here.

143
00:11:01,930 --> 00:11:12,310
Someone put a five for this and then I'll also d reference my dynamic pointer and I will store.

144
00:11:13,460 --> 00:11:19,130
The value seven at that address, so five is being stored at this address.

145
00:11:19,460 --> 00:11:21,680
The pointer is pointing to this variable.

146
00:11:21,680 --> 00:11:23,480
So for example.

147
00:11:25,430 --> 00:11:28,580
The first time in my scrawl, rough, so.

148
00:11:30,600 --> 00:11:34,290
This first time right here, Neal, this was the address, of course, when I compile this and read

149
00:11:34,290 --> 00:11:34,780
it again.

150
00:11:34,800 --> 00:11:38,280
It could be a different address and most likely will be a different address.

151
00:11:40,280 --> 00:11:44,660
Here it was the same, you know, but you know, it could be different, it might not be, you know,

152
00:11:44,660 --> 00:11:47,090
it's this address was different from my dynamic corner.

153
00:11:48,710 --> 00:11:55,460
So I'm going to go ahead and now print this stuff out again so we can see how it's changed.

154
00:11:57,530 --> 00:12:02,840
So what I'm going to do is just copy only these two right here, because these are the lines where I

155
00:12:02,840 --> 00:12:08,720
print out, Oops, not those two, it'll be this one and this one right here.

156
00:12:08,990 --> 00:12:11,630
So let me do this one first, actually.

157
00:12:16,060 --> 00:12:16,630
All right.

158
00:12:16,870 --> 00:12:23,970
So I'm going to print this out and then I am going to I'm sorry, it's so slow here.

159
00:12:23,970 --> 00:12:29,530
I still have that wrist injury, so it's a little hard to do things quickly.

160
00:12:30,280 --> 00:12:30,580
All right.

161
00:12:30,580 --> 00:12:32,710
So we're going to go ahead and save this.

162
00:12:32,710 --> 00:12:38,080
I'm actually going to clear and compile and run again.

163
00:12:39,750 --> 00:12:44,520
All right, so now you notice that we actually have these values stored in their.

164
00:12:46,290 --> 00:12:48,410
So originally we had some.

165
00:12:49,480 --> 00:12:55,000
Some different numbers stored in here, you know, like these are just these garbage values.

166
00:12:55,570 --> 00:13:03,310
And it looks like our one on the stack is actually the same as the previous previous times that we compiled

167
00:13:03,310 --> 00:13:03,800
and ran it.

168
00:13:04,690 --> 00:13:11,590
But here we have ah, you know, the point is, is that both of them have these garbage values.

169
00:13:11,590 --> 00:13:17,290
And then we notice down here that it is actually now storing the things that we put in there.

170
00:13:17,350 --> 00:13:20,560
And that is that happened right here on Line 13 or 14.

171
00:13:21,430 --> 00:13:29,200
So we referenced my pointer, which gave us access to the memory at the address, right?

172
00:13:29,210 --> 00:13:34,450
So this address six one, you know, Hex six one two five zero.

173
00:13:35,050 --> 00:13:42,910
We were now able to do reference the memory at that address and take this five and store it there at

174
00:13:42,910 --> 00:13:43,960
that location.

175
00:13:44,990 --> 00:13:51,620
And same for the dynamic memory allocation, one on the heap, we were able to take this seven and store

176
00:13:51,620 --> 00:13:55,160
it at this address, and they are no longer these values.

177
00:13:55,160 --> 00:13:57,350
They are now these values, right?

178
00:14:00,670 --> 00:14:02,470
So pretty cool.

179
00:14:02,950 --> 00:14:03,940
You know, pointers.

180
00:14:04,060 --> 00:14:05,830
Hopefully, it's clearing up a bit.

181
00:14:05,860 --> 00:14:07,870
They're not that complicated.

182
00:14:08,680 --> 00:14:19,420
One important thing, though, about the dynamically allocated memory pointer is that we have to explicitly

183
00:14:19,420 --> 00:14:24,100
delete that memory, which means allocate that memory.

184
00:14:24,790 --> 00:14:26,230
And why do we have to do that?

185
00:14:26,260 --> 00:14:32,320
Well, in the last lecture, I briefly mentioned it towards the end of the lecture that if we do not

186
00:14:32,710 --> 00:14:39,580
allocate that memory, it kind of remains taken up.

187
00:14:39,970 --> 00:14:42,400
You know, it's it's reserved.

188
00:14:42,400 --> 00:14:46,240
I think I use the word reserved so that memory is stays reserved.

189
00:14:46,240 --> 00:14:47,050
It's kind of like.

190
00:14:48,200 --> 00:14:53,810
Think of it like a restaurant or something, maybe you can use this analogy so, you know, someone

191
00:14:53,810 --> 00:14:57,760
reserves a table and you're going to put these people at that table.

192
00:14:57,770 --> 00:14:58,820
It's a location, right?

193
00:14:58,820 --> 00:15:00,560
That table is a specific location.

194
00:15:00,560 --> 00:15:06,080
You put people in food at that table, then you know you're done using that table.

195
00:15:06,080 --> 00:15:10,730
The restaurant is done, the people leave and you know you.

196
00:15:10,760 --> 00:15:16,040
But what happens is that you leave all the food on the table and in fact, you leave the the reserved

197
00:15:16,880 --> 00:15:18,680
placard or something on the table.

198
00:15:18,680 --> 00:15:21,530
So the table still says Reserve, but the table isn't reserved.

199
00:15:22,640 --> 00:15:27,530
But now you can't use that, and let's say you're getting like a massive influx of people into this

200
00:15:27,530 --> 00:15:30,770
restaurant, like you're just having to suddenly really busy.

201
00:15:31,310 --> 00:15:33,470
And let's think of the think of.

202
00:15:35,320 --> 00:15:37,570
You know, maybe you did this multiple times, like.

203
00:15:38,480 --> 00:15:44,610
You didn't only forget to clean the table and take the reserved sign off of one table.

204
00:15:44,630 --> 00:15:49,880
What if you did that for like 20 tables or something like that and now you're having tons of people

205
00:15:49,880 --> 00:15:50,540
coming in.

206
00:15:51,290 --> 00:15:54,800
All of those tables are still taken up and you have very little space.

207
00:15:54,800 --> 00:16:01,850
So you can imagine that if you leave a lot of memory on the heap allocated, of course, it's a really

208
00:16:01,850 --> 00:16:03,470
big area of memory.

209
00:16:05,870 --> 00:16:12,020
But if you have enough data that you need to now store, you might not have enough space, right?

210
00:16:12,050 --> 00:16:13,460
That is a potential thing.

211
00:16:13,460 --> 00:16:15,770
You're running out of space and you're limiting yourself.

212
00:16:16,490 --> 00:16:22,130
So that's kind of the main reason why it's not good to have memory leaks.

213
00:16:22,130 --> 00:16:29,120
And a memory leak is when you do not allocate the dynamically allocated memory, which you did right

214
00:16:29,120 --> 00:16:29,620
here, right?

215
00:16:29,640 --> 00:16:30,500
We did it right here.

216
00:16:30,500 --> 00:16:34,280
We made some dynamically allocated memory right here and have a pointer to it.

217
00:16:35,340 --> 00:16:38,220
If we do not delete this, it remains reserved.

218
00:16:38,790 --> 00:16:46,320
And if you can imagine if you multiply this by a very large number of times, you could potentially

219
00:16:46,590 --> 00:16:49,290
not have enough space for your program.

220
00:16:51,060 --> 00:16:58,290
And this is really only in the kind of realm of you running your program, right?

221
00:16:58,320 --> 00:17:03,930
So when you run your program, your program gets loaded into memory and then you start having these

222
00:17:04,440 --> 00:17:06,020
chunks of space taken up, right?

223
00:17:06,030 --> 00:17:13,250
So once you're done running your program, these memory leaks are no longer really a problem.

224
00:17:13,260 --> 00:17:17,850
Like it's not like they remain on your system like forever or something like that.

225
00:17:17,850 --> 00:17:19,010
So don't worry about that.

226
00:17:19,020 --> 00:17:28,050
But if you are running a program continuously, then these memory resources become quite valuable and

227
00:17:28,050 --> 00:17:31,530
they can run out of space if you were to have a ton and ton of memory leaks.

228
00:17:31,590 --> 00:17:31,890
Right?

229
00:17:32,340 --> 00:17:33,780
So that's kind of the main point.

230
00:17:34,050 --> 00:17:35,940
That's the bad thing about memory leaks.

231
00:17:36,720 --> 00:17:38,040
So let's go ahead and do that.

232
00:17:38,040 --> 00:17:39,900
Let's make sure that we did allocate that.

233
00:17:39,900 --> 00:17:43,200
It's going to be as simple as using this word delete.

234
00:17:44,070 --> 00:17:49,530
So I say delete highlights there, and I just say delete my dynamic pointer.

235
00:17:50,920 --> 00:17:58,920
And that's all you have to do in C++, and it will d allocate this memory that was set aside right here,

236
00:17:58,930 --> 00:18:03,130
you noticed that my dynamic pointer was just an address, right?

237
00:18:03,700 --> 00:18:11,050
But fortunately, C++ knows what to do with this delete keyword.

238
00:18:11,050 --> 00:18:13,930
So it sees this my dynamic pointer.

239
00:18:13,930 --> 00:18:18,010
And even though it's an address, it's not going to just delete the address.

240
00:18:18,040 --> 00:18:25,750
It's not just going to delete the pointer, it's going to actually go and delete and clear up the memory

241
00:18:25,750 --> 00:18:28,060
at that point it right at that address.

242
00:18:29,860 --> 00:18:31,930
That the pointer is pointed to, that's what I mean.

243
00:18:33,930 --> 00:18:35,460
So pretty cool.

244
00:18:36,030 --> 00:18:44,880
I think that I'm going to kind of leave this first lecture at this, so I don't stuff too much into

245
00:18:44,880 --> 00:18:52,380
it, but in the next few lectures, we're going to continue on looking at some pointers and their use

246
00:18:52,380 --> 00:18:52,980
cases.

247
00:18:52,980 --> 00:18:54,900
We will use some functions.

248
00:18:55,980 --> 00:18:57,000
And stuff like that.

249
00:18:57,390 --> 00:18:59,100
Look at returning pointers.

250
00:18:59,340 --> 00:19:05,220
I will use some other data types, like a string, just to kind of show you that it's the same way we'll

251
00:19:05,220 --> 00:19:11,700
look at, you know, in another way to initialize dynamic memory dynamically allocated memory pointers.

252
00:19:12,240 --> 00:19:18,330
And we will also talk about using dynamic memory with arrays as well.

253
00:19:18,720 --> 00:19:19,860
So future topics.

254
00:19:19,860 --> 00:19:26,970
But hopefully this is enough to just help you understand what pointers really are as far as you writing

255
00:19:26,970 --> 00:19:35,820
code for them and the difference between a pointer that might have memory, that's that's on the stack

256
00:19:36,000 --> 00:19:40,650
compared to a pointer that is pointing to something on the heap.

257
00:19:43,080 --> 00:19:43,770
All right.

258
00:19:43,800 --> 00:19:45,690
So with that, I will see you in the next election.
