1
00:00:00,840 --> 00:00:01,380
OK.

2
00:00:01,830 --> 00:00:08,910
So in this lecture, we're going to take a pretty serious deep dive into virtual memory.

3
00:00:09,090 --> 00:00:14,070
I had one lecture that kind of just barely introduce you to a virtual memory was.

4
00:00:15,000 --> 00:00:20,400
But we're going to expand upon that quite a bit and we're going to introduce some new topics, too.

5
00:00:20,410 --> 00:00:24,570
So we've kind of already talked about process virtual memory a little bit, but we're going to expand

6
00:00:24,570 --> 00:00:24,930
on that.

7
00:00:25,560 --> 00:00:31,560
I have mentioned addresses, you know, the hex addresses that were kind of like the parking lot space

8
00:00:31,560 --> 00:00:32,130
numbers.

9
00:00:32,170 --> 00:00:39,240
Remember that analogy where I was talking about pieces of memory, like the addresses of memory being

10
00:00:39,240 --> 00:00:43,140
like parking spots that had numbers to label them?

11
00:00:43,680 --> 00:00:47,280
So we're going to kind of expand on that as well and look deeper into it.

12
00:00:48,390 --> 00:00:52,890
I'm going to introduce a new topic called pointers and also a new topic called dynamic memory allocation.

13
00:00:52,890 --> 00:00:58,350
So if you already know what these are but want to hear a little more about them, just want to like,

14
00:00:58,690 --> 00:00:59,610
say that up front.

15
00:00:59,610 --> 00:01:01,410
So you know that that is in this lecture.

16
00:01:02,790 --> 00:01:06,320
So here is our virtual address space.

17
00:01:06,330 --> 00:01:13,800
So think back to your favorite analogy, whether it be the parking lot with the numbered like spots,

18
00:01:13,800 --> 00:01:14,100
right?

19
00:01:14,130 --> 00:01:15,120
These are the numbers.

20
00:01:15,120 --> 00:01:17,910
So these are considered addresses, right?

21
00:01:17,940 --> 00:01:19,110
Memory addresses.

22
00:01:19,350 --> 00:01:27,150
You could also think about another analogy like a bunch of maybe like houses lined up on on a road or

23
00:01:27,150 --> 00:01:29,310
something like that right next to each other.

24
00:01:30,840 --> 00:01:37,110
But the important another important thing to remember is that each car that would be parked here, each

25
00:01:37,110 --> 00:01:39,870
space, actually, I should say each parking space.

26
00:01:40,870 --> 00:01:43,240
Is eight minutes long or one bite?

27
00:01:43,780 --> 00:01:48,220
So if you want to think of it like that, like a car, you know it have to be a car, a car could go

28
00:01:48,220 --> 00:01:48,430
there.

29
00:01:48,430 --> 00:01:53,110
There was maximum eight bits size, one bite and size.

30
00:01:53,140 --> 00:01:54,910
Same with like a house, you know?

31
00:01:55,270 --> 00:01:58,540
That's how much space is there at one specific address.

32
00:01:59,920 --> 00:02:03,880
We also have our high address hex number here, so this is the like.

33
00:02:03,910 --> 00:02:07,360
We would consider this like the highest address and the lowest address.

34
00:02:07,360 --> 00:02:16,390
We may not actually use all zeros like we may not use that address literally when we're managing memory

35
00:02:16,390 --> 00:02:17,260
and stuff like that.

36
00:02:17,260 --> 00:02:20,680
But I just want to put that, you know, this is low, this is high.

37
00:02:20,680 --> 00:02:24,400
We don't really care about the dark grey here or here.

38
00:02:24,610 --> 00:02:31,750
What we care about is this right here when we run our program, it is loaded into memory in this area.

39
00:02:32,230 --> 00:02:36,040
This point of the arrow to this point of the area, that arrow.

40
00:02:36,160 --> 00:02:38,890
It is this light gray chunk here.

41
00:02:39,760 --> 00:02:47,710
OK, so we run our program, our program gets loaded into this virtual address space, this virtual

42
00:02:47,710 --> 00:02:48,700
memory space.

43
00:02:48,730 --> 00:02:51,330
This is like our process virtual memory.

44
00:02:52,500 --> 00:02:52,870
All right.

45
00:02:52,890 --> 00:03:00,270
And this is like you can think of this as like somewhere in RAM, it is some volatile storage locations

46
00:03:00,270 --> 00:03:02,580
and volatile memory location.

47
00:03:02,670 --> 00:03:03,030
Right?

48
00:03:03,510 --> 00:03:05,190
But this is a virtual thing.

49
00:03:05,190 --> 00:03:06,430
It's not a physical thing.

50
00:03:06,450 --> 00:03:11,220
We're just talking about this virtual imaginary space where our program is stored, right?

51
00:03:13,260 --> 00:03:15,870
So what is it the bottom of this thing?

52
00:03:15,870 --> 00:03:19,440
We're going to talk about the different pieces that make up this light gray.

53
00:03:20,010 --> 00:03:25,270
This is something called our tech segment, and it literally holds the text code that we wrote, right?

54
00:03:25,320 --> 00:03:30,750
We typed out a bunch of characters for our C++ code, and this is where it gets stored here at the bottom.

55
00:03:32,360 --> 00:03:36,680
The next up is something called global variables, I haven't really explained what the global variable

56
00:03:36,680 --> 00:03:42,110
is, but basically it's just like if you declared a variable at the top of a program above the main

57
00:03:42,110 --> 00:03:47,330
function, above the other functions that might be above main.

58
00:03:47,900 --> 00:03:49,100
It's just not in a function.

59
00:03:49,100 --> 00:03:51,050
So it's not local to a function.

60
00:03:51,050 --> 00:03:54,650
It's not a variable that's only known by one function.

61
00:03:55,070 --> 00:04:01,820
A global variable means, you know, in the sense that global available to everything globally, available

62
00:04:01,820 --> 00:04:04,910
to all of the functions, all the parts of the code.

63
00:04:05,360 --> 00:04:07,970
But we're going to talk about global variables later on.

64
00:04:08,270 --> 00:04:11,390
I just wanted to point out that that stuff was stored right here.

65
00:04:11,420 --> 00:04:14,930
There's also some other things stored right here that I don't want to get into right now because I feel

66
00:04:14,930 --> 00:04:17,600
like you just overcomplicate what I'm trying to get across.

67
00:04:18,920 --> 00:04:20,030
What's after that?

68
00:04:20,570 --> 00:04:21,200
So.

69
00:04:21,230 --> 00:04:22,480
Oh yeah, another important thing.

70
00:04:22,490 --> 00:04:23,330
These are two.

71
00:04:23,570 --> 00:04:26,630
Generally, these two areas are generally a fixed size.

72
00:04:26,810 --> 00:04:29,330
So this is something that's determined at compile time.

73
00:04:29,330 --> 00:04:34,520
So when the program was compiled, when the program gets ran, it's not like these are going to change

74
00:04:34,520 --> 00:04:34,970
size.

75
00:04:34,970 --> 00:04:36,890
So that's why I wanted to point that out as well.

76
00:04:37,040 --> 00:04:42,750
These two sections right here, something that does grow in size is the heap.

77
00:04:42,770 --> 00:04:48,360
So that is an area right above here that grows upward towards higher memory addresses.

78
00:04:48,770 --> 00:04:50,570
More explanation on that in a second.

79
00:04:51,170 --> 00:04:55,950
We also have this thing that starts from the top and grows down, which is called the stack.

80
00:04:55,970 --> 00:05:00,800
It grows towards lower memory addresses, so let's go ahead and check out what these two things are.

81
00:05:00,830 --> 00:05:01,970
First, we'll look at the stack.

82
00:05:03,020 --> 00:05:11,090
So the stack is primarily where the data from our function calls go, and I don't want to confuse you

83
00:05:11,090 --> 00:05:13,190
with just saying function call.

84
00:05:13,580 --> 00:05:20,210
It's really the stuff that you would see inside your function definition that would be stored here on

85
00:05:20,210 --> 00:05:23,540
the stack for each one of your functions.

86
00:05:23,900 --> 00:05:30,440
The fact is, though, is that those things don't really go onto the stack and tell the function gets

87
00:05:30,440 --> 00:05:31,580
called intel.

88
00:05:32,270 --> 00:05:36,740
The execution of your C++ code goes from the function.

89
00:05:36,740 --> 00:05:39,140
Call into that function definition.

90
00:05:39,920 --> 00:05:45,950
Once that happens, you know, once it sees the function call, it basically sets aside a new little

91
00:05:45,950 --> 00:05:47,930
chunk of memory here on the stack.

92
00:05:48,320 --> 00:05:52,610
When another function is called, it sets another thing aside, and it looks kind of like this.

93
00:05:53,270 --> 00:05:54,710
Here's one function call.

94
00:05:54,950 --> 00:05:56,420
Here's another function call.

95
00:05:56,630 --> 00:05:58,070
Here's another function call.

96
00:05:58,310 --> 00:06:01,250
Here's another function call and it kind of stacks up.

97
00:06:01,250 --> 00:06:06,290
It's almost looks like plate stacking on top of each other, but like stacking downwards, right?

98
00:06:07,070 --> 00:06:09,470
These things are called stack frames.

99
00:06:09,470 --> 00:06:10,550
These green things right here.

100
00:06:10,580 --> 00:06:17,600
So whenever a function call happens, it's represented in memory as a stack frame and it goes on the

101
00:06:17,660 --> 00:06:19,610
stack, hence the name stack frame.

102
00:06:20,590 --> 00:06:27,040
So these frames are these little chunks, these rectangles here, they hold data that is local to the

103
00:06:27,040 --> 00:06:28,090
function scope.

104
00:06:28,510 --> 00:06:34,630
So stuff inside the curly braces of the function definition like variables that you may put in there,

105
00:06:34,810 --> 00:06:42,190
as well as the parameters that are inside of the function definition parentheses on that first line

106
00:06:42,190 --> 00:06:42,730
of the function.

107
00:06:42,730 --> 00:06:44,020
Different definition, right?

108
00:06:44,950 --> 00:06:48,670
So if you have those parameters, those will be pushed onto the stack as well.

109
00:06:49,850 --> 00:06:51,860
All right, cool.

110
00:06:52,970 --> 00:07:01,430
So something interesting is when the stack rose downward too far, it can overwrite memory in another

111
00:07:01,430 --> 00:07:03,590
section like we had the heap right here, right?

112
00:07:04,370 --> 00:07:10,850
This results in something called Stack Overflow, and this can happen like a commonly in this happening

113
00:07:11,690 --> 00:07:14,780
is when you have, like, you know, too much recursion.

114
00:07:15,050 --> 00:07:16,970
Remember, we just talked about recursion.

115
00:07:17,480 --> 00:07:22,550
We're not going to go deep into that until the second segment of the course, which is the data structures

116
00:07:22,550 --> 00:07:23,540
and algorithms segment.

117
00:07:23,540 --> 00:07:30,560
But I did show you that if you were to accidentally arrange like function calls in another function

118
00:07:30,560 --> 00:07:32,810
call, it puts you in kind of a loop.

119
00:07:33,470 --> 00:07:36,440
Then it went infinitely kind of like the infinite wow loop.

120
00:07:36,440 --> 00:07:42,470
Remember from that lecture where the last like, I don't know was the last lecture election before?

121
00:07:43,680 --> 00:07:47,910
We were having a function, a call, function B, call function C, stuff like that.

122
00:07:47,940 --> 00:07:53,910
Well, if you had it function B call itself, it would just bounce around inside of itself and it would

123
00:07:53,910 --> 00:07:54,720
never exit.

124
00:07:55,290 --> 00:08:02,450
That can eventually lead to a stack overflow as well as you having some kind of indirect recursion.

125
00:08:02,470 --> 00:08:10,260
I talked about that as well, which is kind of like know because a and then and then a calls B and then

126
00:08:10,260 --> 00:08:14,490
in a sense, be called a just calls again, you know, and just keeps going back and forth like that.

127
00:08:14,490 --> 00:08:15,530
That's indirect recursion.

128
00:08:15,540 --> 00:08:18,480
So just want to point that out, something interesting.

129
00:08:18,780 --> 00:08:25,470
There's also a popular site where you can look at how to solve errors and stuff is kind of like a forum

130
00:08:25,470 --> 00:08:26,910
is called Stack Overflow.

131
00:08:27,060 --> 00:08:28,320
Really popular website.

132
00:08:28,560 --> 00:08:29,790
Lots of programmers use that.

133
00:08:31,560 --> 00:08:32,730
So let's talk about the heap.

134
00:08:32,730 --> 00:08:38,460
So the heap is a place where we can store something called dynamically allocated memory and variables.

135
00:08:39,030 --> 00:08:40,980
It's not necessarily the only thing on the heap.

136
00:08:41,790 --> 00:08:43,740
In fact, we were talking about global variables.

137
00:08:43,740 --> 00:08:48,120
Sometimes that can be interpreted as being kind of part of the heap, even though we've got it here.

138
00:08:50,040 --> 00:08:54,300
But now we're going to talk about like too much detail about the heap, but it's definitely capable

139
00:08:54,300 --> 00:08:58,510
of storing this dynamically allocated memory flash variables thing.

140
00:08:58,530 --> 00:09:01,320
And that's something we're going to talk about in this lecture a little bit later.

141
00:09:03,330 --> 00:09:09,420
So the heap is useful for storing things that might grow a lot in size.

142
00:09:09,420 --> 00:09:11,910
So we talked about the heap grows in the stack grows right.

143
00:09:12,150 --> 00:09:17,310
The heap is kind of a more open space, though, because it's managed less by the operating system.

144
00:09:18,260 --> 00:09:24,080
So it's a good space for us to store things where we're thinking maybe this could get pretty big, like

145
00:09:24,230 --> 00:09:28,220
we have a container, I think the only container that we've talked about is an array so far.

146
00:09:29,310 --> 00:09:33,510
But if we had a container that was just going to get really, really big when store like a crazy amount

147
00:09:33,510 --> 00:09:36,600
of data in it or we, you know, we just had some.

148
00:09:37,550 --> 00:09:41,390
Thing in our cold, it was going to be big, we might want a story here on the heap where we have more

149
00:09:41,390 --> 00:09:41,870
space.

150
00:09:44,370 --> 00:09:51,000
So let's talk about managing this memory, because C++ is a cool language and we can actually kind of

151
00:09:51,840 --> 00:09:57,090
do lower level stuff and manage the memory so our programs can be more efficient.

152
00:09:58,380 --> 00:10:01,320
So how do we refer to a specific location in memory?

153
00:10:01,330 --> 00:10:06,960
I'm talking about like an address space and memory, you know, some sort of chunk of memory.

154
00:10:08,520 --> 00:10:16,020
We can access memory and kind of refer to memory by using something called a pointer.

155
00:10:16,920 --> 00:10:18,690
So what is a pointer?

156
00:10:18,690 --> 00:10:21,660
And this is something that's a new concept we're going to have to introduce.

157
00:10:23,140 --> 00:10:27,160
So a pointer is just something that holds an address.

158
00:10:28,290 --> 00:10:33,630
So we talked about like having addresses right here, we have the Hex, like with the eight, right?

159
00:10:33,900 --> 00:10:36,300
So imagine you have some hex address here.

160
00:10:37,720 --> 00:10:41,440
We can store like in, let's say, an integer in memory.

161
00:10:41,740 --> 00:10:42,130
Right?

162
00:10:42,550 --> 00:10:44,650
So let's say there's an integer stored in here.

163
00:10:45,400 --> 00:10:52,930
Maybe I want to have some sort of variable that doesn't necessarily store the integer that's here,

164
00:10:53,500 --> 00:10:56,650
but instead it stores the address number.

165
00:10:57,130 --> 00:11:03,490
So later on, I can follow that address number and then find the integer here.

166
00:11:04,300 --> 00:11:06,950
The pointer is the thing that stores that address.

167
00:11:07,600 --> 00:11:11,260
So it's kind of like, you know, storing the number for the parking spot, right?

168
00:11:12,680 --> 00:11:19,280
So you can think of a pointer is basically an integer, although it is seen as a hex address, you know,

169
00:11:19,280 --> 00:11:24,380
we're still seeing like zero x f f f f f, you know, all that stuff.

170
00:11:24,380 --> 00:11:30,890
But you know, a hex number also has a decimal form, which you can think of as an integer.

171
00:11:30,890 --> 00:11:37,370
So you can just think of pointers as just always holding integers right there, just holding an address.

172
00:11:37,370 --> 00:11:41,150
So pointers are often, like really confusing for people.

173
00:11:41,150 --> 00:11:45,650
So, you know, we're going to look more into this, but I'm trying to like make it as simple as possible.

174
00:11:45,650 --> 00:11:53,300
So just think of pointers as integers or just addresses like it's just a holder, it's like a variable

175
00:11:53,300 --> 00:11:55,430
that holds the address, right?

176
00:11:55,820 --> 00:12:02,000
And you can use that address that's stored in the pointer to find what is stored there at that location

177
00:12:02,000 --> 00:12:03,050
at that address, right?

178
00:12:04,610 --> 00:12:07,100
So let's talk about how to use these pointers.

179
00:12:07,100 --> 00:12:11,990
So to use pointers, we need to use some symbols that we're familiar with, but we're not going to be

180
00:12:11,990 --> 00:12:14,900
using them in the same way we're going to have to use them in a different way.

181
00:12:15,840 --> 00:12:19,200
So what you see right here is a main function.

182
00:12:20,540 --> 00:12:27,080
So inside this main function, what we're doing is we're making a new integer a we are assigning the

183
00:12:27,080 --> 00:12:28,580
value seven to a.

184
00:12:29,270 --> 00:12:32,840
So make a new integer a sign the value seven to it.

185
00:12:33,660 --> 00:12:40,920
And then this is going to go on the stack because it's inside of a function.

186
00:12:41,220 --> 00:12:43,140
This is the main function, so.

187
00:12:44,480 --> 00:12:47,960
Talking about pointers now, but since we're just talking about the stack, I wanted to go ahead and

188
00:12:47,960 --> 00:12:52,430
throw this in there and let you know that the things inside the function are going to get stored on

189
00:12:52,430 --> 00:12:52,790
the stack.

190
00:12:52,790 --> 00:12:53,360
So these.

191
00:12:54,380 --> 00:12:59,210
I shouldn't say things inside the function, these local variables that are local to the function will

192
00:12:59,210 --> 00:13:01,340
be stored on the stack.

193
00:13:01,730 --> 00:13:02,090
All right.

194
00:13:03,270 --> 00:13:03,690
So.

195
00:13:05,220 --> 00:13:07,230
Next, what we do is kind of weird.

196
00:13:07,680 --> 00:13:12,750
We use this multiplication symbol, it's the asterisk, but we're not multiplying anything.

197
00:13:13,410 --> 00:13:21,660
What we're doing here is making a pointer called B, and we're making this pointer point to the address

198
00:13:22,710 --> 00:13:23,490
of A.

199
00:13:24,360 --> 00:13:31,700
So obviously, seven was stored on the stack at some memory location, right?

200
00:13:31,720 --> 00:13:34,530
It was stored in that memory space somewhere.

201
00:13:35,870 --> 00:13:42,440
And a holds that value seven, if we want to get the address.

202
00:13:43,410 --> 00:13:49,590
Of a though, and not just the value, we need to use this little ampersand here, right?

203
00:13:50,040 --> 00:13:57,960
This ampersand here is something we've seen we've seen using two ampersand for the logical and operator,

204
00:13:58,230 --> 00:14:03,260
and we've used one ampersand like this for pass by reference.

205
00:14:04,590 --> 00:14:09,420
In fact, pointers are kind of related a little bit to pass by reference, so when you pass that thing

206
00:14:09,420 --> 00:14:15,990
by pass a very BioReference member, we talked about how it really passes that actual memory location.

207
00:14:16,740 --> 00:14:19,320
It's such that it's like a reference.

208
00:14:19,320 --> 00:14:26,220
It's a constant reference to that memory location, like an alias to it, you know, or something like

209
00:14:26,220 --> 00:14:26,550
that.

210
00:14:26,670 --> 00:14:28,680
So this is pretty similar.

211
00:14:29,310 --> 00:14:31,290
It's getting us the address, right?

212
00:14:31,290 --> 00:14:32,790
It's referring to a.

213
00:14:32,910 --> 00:14:37,740
But when you see that, see it in this position here and we're not passing it as a parameter.

214
00:14:38,040 --> 00:14:41,180
You can just think of the ampersand as giving us the address of a.

215
00:14:44,860 --> 00:14:54,730
So like I said, the star signifies that B is a pointer, this star right here next to the B and the

216
00:14:54,730 --> 00:15:01,690
fact that we have an integer in front of it basically says that we're making a pointer that points to

217
00:15:01,690 --> 00:15:02,380
an integer.

218
00:15:02,890 --> 00:15:08,020
The end part is kind of irrelevant, though the only reason we're doing that is just because, like

219
00:15:08,020 --> 00:15:13,570
we said, it points to an area of memory that's most likely storing the data type integer.

220
00:15:14,110 --> 00:15:16,630
So that's why we put Int Pointer B.

221
00:15:18,090 --> 00:15:25,950
And like I said, the end is used being used in a way that enable us to grab the address of a, as he

222
00:15:25,950 --> 00:15:26,930
put it, before the A.

223
00:15:29,190 --> 00:15:30,780
So but what's the point?

224
00:15:30,840 --> 00:15:33,370
Hah, gotcha, funny.

225
00:15:33,410 --> 00:15:34,350
Use the word point.

226
00:15:35,570 --> 00:15:43,700
So similar to the past by referencing the pointer refers to the actual piece of memory, and what we

227
00:15:43,700 --> 00:15:50,510
want is to potentially reference that from another piece of code.

228
00:15:51,440 --> 00:15:51,890
So.

229
00:15:53,010 --> 00:16:01,170
Think about a scenario where we store an array somewhere in memory, right, so we have an array and

230
00:16:01,320 --> 00:16:05,400
that array has very large elements, it has very large items.

231
00:16:05,970 --> 00:16:12,810
And later on, we're going to be interested in to we're going to be interested in referring back to

232
00:16:12,810 --> 00:16:13,440
this array.

233
00:16:14,840 --> 00:16:20,660
So we have, you know, the items that are large stored in a large array is taken up a large piece of

234
00:16:20,660 --> 00:16:21,170
memory.

235
00:16:23,850 --> 00:16:30,660
So now imagine we want to fill another array later on in our code with a bunch of copies of the stuff

236
00:16:30,660 --> 00:16:32,250
that was in this other era.

237
00:16:32,310 --> 00:16:32,650
Right.

238
00:16:32,670 --> 00:16:35,250
So we want the same items that are in the other array.

239
00:16:36,210 --> 00:16:41,280
We could go ahead and copy that stuff over to this new era.

240
00:16:41,550 --> 00:16:45,120
You know, it's going to be another huge array with huge items in it.

241
00:16:45,810 --> 00:16:53,790
Or instead, we could just store the addresses to those original elements or items in that other array.

242
00:16:54,180 --> 00:17:00,510
And that would take up a lot less space and a lot of cases, because if the items are like some huge

243
00:17:00,510 --> 00:17:04,820
data type imagines, maybe it's a data type we haven't talked about yet.

244
00:17:05,070 --> 00:17:10,620
We're going to make our own data types later on, which might contain a ton of stuff, and they take

245
00:17:10,620 --> 00:17:11,670
up a lot of space.

246
00:17:12,570 --> 00:17:15,570
It's going to be better to just store an integer, right?

247
00:17:16,380 --> 00:17:19,830
We talked about the point of just being integer right, but it's like a hex address.

248
00:17:20,490 --> 00:17:29,700
It's a lot cheaper, less costly, less space to store and address than to like copy over the whole

249
00:17:29,700 --> 00:17:32,390
like huge item like item by item, right?

250
00:17:32,550 --> 00:17:39,420
We could just store have an array of pointers to those items, right, that refer to the original items.

251
00:17:39,990 --> 00:17:42,990
So pointers can save you a lot of space.

252
00:17:43,020 --> 00:17:49,230
And that's nice because when you write really large programs, honestly, space becomes a huge factor

253
00:17:49,230 --> 00:17:54,360
in the efficiency of your product or your software that you're writing.

254
00:17:57,200 --> 00:18:03,980
So if we are going to be storing big things, though, we should really be storing them on the heap

255
00:18:03,980 --> 00:18:08,600
because remember, we were talking about the heap having a lot more open space available for us.

256
00:18:09,170 --> 00:18:15,470
A caveat, though, is since the heap is not really managed the same way as the stack, the operating

257
00:18:15,470 --> 00:18:19,670
system isn't doing as much stuff managing that region of memory.

258
00:18:20,210 --> 00:18:23,290
We're going to have to manage that region of memory ourselves.

259
00:18:23,360 --> 00:18:25,490
We have to manage to keep memory ourselves.

260
00:18:25,910 --> 00:18:33,710
This means specifically allocating and allocating memory for our variables and using pointers to keep

261
00:18:33,740 --> 00:18:34,490
track of them.

262
00:18:35,420 --> 00:18:41,630
If you don't know what allocating and allocating means, like in the sense of memory is similar to just

263
00:18:41,630 --> 00:18:46,250
like allocate, when you say allocating is just coming up with space for stuff.

264
00:18:47,160 --> 00:18:55,960
Right, like, I allocate some space in the drawer for my clothes or something, right like right,

265
00:18:55,990 --> 00:18:59,040
I free up some space are not free.

266
00:18:59,050 --> 00:19:02,040
Sorry, I set aside some space in the drawer.

267
00:19:03,220 --> 00:19:09,130
And freeing up space would be allocating, so basically when you allocate memory, it's basically like

268
00:19:09,130 --> 00:19:17,770
you're putting stuff in memory, you're you're setting aside some room and then storing stuff there.

269
00:19:18,340 --> 00:19:18,710
Right?

270
00:19:18,730 --> 00:19:25,480
So you're you have to make space, you make space when you're allocating and then allocating is freeing

271
00:19:25,480 --> 00:19:26,140
up that space.

272
00:19:26,140 --> 00:19:27,550
So making it available again.

273
00:19:28,540 --> 00:19:32,170
I guess that wasn't a very good analogy, maybe a good analogy is kind of like.

274
00:19:33,720 --> 00:19:38,760
You know, I I when you set aside space, you're saying, OK, this space is reserved.

275
00:19:39,000 --> 00:19:41,010
I'm reserving space for stuff.

276
00:19:41,130 --> 00:19:42,660
And that's when you allocate, right?

277
00:19:43,500 --> 00:19:48,890
And then when it's no longer reserved, you can allocate it to you like, Oh, it's free, it's vacant.

278
00:19:48,900 --> 00:19:50,190
We have a vacancy now.

279
00:19:50,190 --> 00:19:51,300
It was reserved.

280
00:19:51,480 --> 00:19:52,290
Now it's vacant.

281
00:19:53,010 --> 00:19:53,310
Right?

282
00:19:54,090 --> 00:19:55,290
Maybe that's a better analogy.

283
00:19:56,780 --> 00:20:01,490
And we're going to have to use pointers to do this because pointers are going to help us manage that

284
00:20:01,490 --> 00:20:02,060
heat memory.

285
00:20:03,870 --> 00:20:06,960
So we're going to take a look back at the last point, for example.

286
00:20:07,650 --> 00:20:13,320
But instead of storing a and B on the stack, we're going to store it on the heap.

287
00:20:13,500 --> 00:20:16,530
And in fact, I think in my example, I'm not going to really store B on the heap.

288
00:20:16,540 --> 00:20:17,670
I'm just going to store a.

289
00:20:18,690 --> 00:20:23,430
So we're going to need to add some additional syntax to make stuff go on the heap.

290
00:20:25,910 --> 00:20:29,750
So this is what it's going to look like to store a on the heap rather than a stack.

291
00:20:29,780 --> 00:20:33,560
We're going to need to use some additional syntax right here.

292
00:20:35,420 --> 00:20:47,480
So what we're doing is on this line, we are making a pointer that contains the address of a and we're

293
00:20:47,750 --> 00:20:55,580
making we're basically putting aside some space that allocation on the heap right here.

294
00:20:56,300 --> 00:21:02,360
So this part and Star A says this is now a pointer containing the address of A.

295
00:21:02,630 --> 00:21:06,980
And then on the heap part is basically this new entry here.

296
00:21:07,790 --> 00:21:10,490
This new is the real key word.

297
00:21:10,490 --> 00:21:11,960
It's a C++ keyword.

298
00:21:12,590 --> 00:21:17,360
This you can you can basically look at this new and every time you see new, you can make up a heap

299
00:21:17,510 --> 00:21:18,290
that means heap.

300
00:21:18,890 --> 00:21:25,550
If I use the keyword new, it's dynamically allocating memory, which means it's going to be on the

301
00:21:25,550 --> 00:21:25,910
heap.

302
00:21:26,330 --> 00:21:29,630
New is the keyword for dynamic memory allocation.

303
00:21:30,600 --> 00:21:38,550
New, it is going to set aside and reserve some space on the heap for an integer variable a specifically

304
00:21:38,550 --> 00:21:42,690
since we talked about this since we defined this right here.

305
00:21:43,600 --> 00:21:49,030
And Start A is just the syntax to say, Hey, I want to make a pointer.

306
00:21:50,260 --> 00:21:51,700
With the name A.

307
00:21:53,070 --> 00:21:55,920
That is going to point to an integer, right?

308
00:21:57,390 --> 00:22:00,300
This line down here is something interesting.

309
00:22:00,840 --> 00:22:09,990
So the star here isn't as like re declaring a pointer, it's actually something called D referencing

310
00:22:09,990 --> 00:22:10,560
a.

311
00:22:11,100 --> 00:22:14,430
So what we're doing is we are kind of.

312
00:22:16,890 --> 00:22:19,290
Following it to the address and.

313
00:22:20,270 --> 00:22:26,600
Opening up that memory kind of like clearing a path to that memory, and then we're assigning seven

314
00:22:26,600 --> 00:22:28,520
to that memory spot.

315
00:22:29,880 --> 00:22:33,750
So a is a pointer, so it stores an address, right?

316
00:22:34,860 --> 00:22:38,880
You can't just say a equals seven without the star.

317
00:22:39,210 --> 00:22:45,540
And the reason you can't do that is because, well, technically I don't want to say you can't, you

318
00:22:45,540 --> 00:22:47,090
could, but you don't want to.

319
00:22:47,100 --> 00:22:48,750
And the reason you don't want to.

320
00:22:49,200 --> 00:22:53,370
Is because this is the thing that we want to store.

321
00:22:53,970 --> 00:22:56,400
Yet a holds the address.

322
00:22:56,910 --> 00:23:03,780
We don't want to put the thing that we want to store in memory and overwrite the address.

323
00:23:03,990 --> 00:23:06,900
We want to put it at the location of the address.

324
00:23:07,140 --> 00:23:11,660
And if you want to put it at the location of the address, you have to put a little star in front of

325
00:23:11,660 --> 00:23:19,890
the A, which the references it and clears an open path for you to toss this seven into that open memory

326
00:23:19,890 --> 00:23:20,340
slot.

327
00:23:20,820 --> 00:23:21,210
All right.

328
00:23:24,160 --> 00:23:29,470
So let's examine it a little closer, some of this I kind of already explained, but I just want to

329
00:23:29,470 --> 00:23:30,130
reiterate.

330
00:23:31,340 --> 00:23:36,410
And Star a US declaring a new pointer that points to an integer.

331
00:23:37,370 --> 00:23:44,240
The new and part on the right side of the equals sign is a specifying that we want to clear new space

332
00:23:44,240 --> 00:23:47,870
so reserved some space for an integer on the heap.

333
00:23:48,920 --> 00:23:52,850
That's why we can also include the end on this side, not only this side.

334
00:23:54,270 --> 00:24:01,890
Star A D references A, which means that it follows the pointer to the data stored at A's address,

335
00:24:01,890 --> 00:24:08,910
except we haven't stored anything at this point until we take the seven and put it there.

336
00:24:09,450 --> 00:24:13,370
That space in memory could have something there.

337
00:24:13,380 --> 00:24:18,420
Remember, we've talked about like garbage values or just kind of floating around in memory, so something

338
00:24:18,420 --> 00:24:19,170
might be there.

339
00:24:19,170 --> 00:24:25,050
But the point is is that we're saying, Hey, this is reserved, and now we're going to put stuff there

340
00:24:25,050 --> 00:24:28,770
and this equals seven is us putting stuff there.

341
00:24:28,860 --> 00:24:33,570
This is almost like, you know, opening the curtains to that memory spot.

342
00:24:33,570 --> 00:24:36,180
And then we put this thing there, right?

343
00:24:36,540 --> 00:24:37,820
Opening the drawer.

344
00:24:38,010 --> 00:24:39,810
So the drawer is open now.

345
00:24:39,810 --> 00:24:42,750
We put the seven in the space that's inside the drawer.

346
00:24:43,730 --> 00:24:50,990
Star, a reference references say for us to be able to do this so we don't overwrite that headdress

347
00:24:50,990 --> 00:24:53,090
that is stored in the a pointer.

348
00:24:53,150 --> 00:24:53,560
Right.

349
00:24:54,890 --> 00:24:55,340
So.

350
00:24:56,490 --> 00:25:04,500
Do referencing a would also give us the ability to access the data stored at AI's address, so if we

351
00:25:04,500 --> 00:25:06,090
wanted to print out.

352
00:25:07,280 --> 00:25:08,780
What is stored at A..

353
00:25:08,810 --> 00:25:11,370
We could see out Star A..

354
00:25:11,390 --> 00:25:16,700
And that would actually print out seven since seven is stored.

355
00:25:17,810 --> 00:25:19,460
They're at that location now.

356
00:25:21,710 --> 00:25:27,960
All right, so that's kind of all I have to say now about this memory allocation stuff.

357
00:25:27,980 --> 00:25:37,850
We are going to practice doing dynamically allocated memory and using pointers more often, but I figure

358
00:25:37,860 --> 00:25:42,930
that it was a good time to just introduce you to these topics.

359
00:25:43,460 --> 00:25:50,240
We won't be using pointers all the time, but it's important to know what they can be used for in the

360
00:25:50,240 --> 00:25:52,490
benefit, right, that they save space.

361
00:25:53,480 --> 00:26:01,460
But I'm going to be honest that it is kind of difficult to use pointers in dynamically allocated memory,

362
00:26:01,470 --> 00:26:03,890
sometimes because you have to manage it.

363
00:26:04,220 --> 00:26:09,230
You remember that I talked about this D allocation part.

364
00:26:09,770 --> 00:26:17,030
This is another thing that we're going to have to talk about when we're using these heap variables and

365
00:26:17,030 --> 00:26:20,690
pointers dynamically allocated memory on the heap.

366
00:26:21,350 --> 00:26:24,080
We're going to need to allocate it as well.

367
00:26:24,110 --> 00:26:30,140
So something that I should just say right off the bat is that any time you see this new keyword, if

368
00:26:30,140 --> 00:26:36,620
you're going to use the word new in your program like this to make something on the heap, just know

369
00:26:36,620 --> 00:26:44,540
that since you did the allocation in this part, everything that gets allocated needs to be allocated,

370
00:26:44,900 --> 00:26:47,210
which means you need to delete that stuff.

371
00:26:47,300 --> 00:26:50,060
So we're going to have to go over that as well in our code.

372
00:26:50,510 --> 00:26:55,460
Just something to keep in mind that, you know, if you're already familiar with some of this stuff,

373
00:26:55,460 --> 00:27:02,570
but you maybe don't know this, you should always allocate any memory that you've allocated with the

374
00:27:02,570 --> 00:27:03,620
key word new.

375
00:27:03,650 --> 00:27:05,810
You need to explicitly delete it.

376
00:27:06,170 --> 00:27:06,680
OK.

377
00:27:07,100 --> 00:27:11,870
Because if you don't do that, you have something called a memory leak.

378
00:27:12,290 --> 00:27:18,290
You've left stuff on the heap and it's filled with all these things that you allocated and never got

379
00:27:18,290 --> 00:27:18,920
rid of.

380
00:27:19,250 --> 00:27:23,050
That can cause problems down the line, and that's something else will get into.

381
00:27:23,930 --> 00:27:24,320
All right.

382
00:27:24,330 --> 00:27:31,670
So with that, I think I'm going to let you go and I will talk to you in the next lecture.
