1
00:00:01,400 --> 00:00:08,900
OK, so welcome back to another one of these lectures about pointers and some dynamic memory stuff in

2
00:00:08,900 --> 00:00:17,090
this lecture, we're going to talk about kind of some weird C++ behavioral stuff that you need to know

3
00:00:17,090 --> 00:00:23,420
about to avoid kind of shooting yourself in the foot with this language, because C++ lets you do a

4
00:00:23,420 --> 00:00:27,140
lot of granular level stuff and it doesn't really protect you.

5
00:00:27,170 --> 00:00:33,050
Like other languages, it'll let you do some things that really just aren't OK, and it shouldn't let

6
00:00:33,050 --> 00:00:33,530
you do.

7
00:00:33,530 --> 00:00:35,750
But it does that to let you have control.

8
00:00:35,990 --> 00:00:40,730
So you can really kind of create sticky situations for yourself, especially with these pointers and

9
00:00:40,730 --> 00:00:41,070
stuff.

10
00:00:41,120 --> 00:00:49,880
So what I'm going to do is I'm going to actually comment out some of this stuff down here, so I'll

11
00:00:49,880 --> 00:00:50,750
just go.

12
00:00:53,340 --> 00:00:55,680
Right here and just kind of block comment this out.

13
00:00:56,370 --> 00:00:58,620
And what I'm going to do is make another function.

14
00:00:59,010 --> 00:01:01,710
What it's going to do is return an end point here.

15
00:01:02,730 --> 00:01:05,700
So I'm going to go ahead and do that.

16
00:01:05,700 --> 00:01:09,780
I'm just going to call this my other function.

17
00:01:10,950 --> 00:01:15,840
And what I'm going to do, though, is I'm going to make a local variable in here and I'm just going

18
00:01:15,840 --> 00:01:16,950
to call this like.

19
00:01:19,160 --> 00:01:23,820
I'll just call, yes, like in local bar five.

20
00:01:25,040 --> 00:01:32,460
And what I'll do is I will return the address.

21
00:01:32,880 --> 00:01:33,220
All right.

22
00:01:33,280 --> 00:01:39,350
So I'm putting this ampersand like we were just talking about the address of the local bar right here.

23
00:01:40,460 --> 00:01:46,280
And then what I'm going to do is I'm going to make another pointer in here and the integer pointer,

24
00:01:46,310 --> 00:01:55,880
and I'm just going to call it main pointer equals my other function because what's going to happen is

25
00:01:55,880 --> 00:02:01,160
it's going to return this address and it's going to take that address and store it in this pointer.

26
00:02:01,910 --> 00:02:10,790
The real problem here, though, there is a problem and that is that or make it a local variable inside

27
00:02:10,790 --> 00:02:15,170
this function and returning the address of that variable.

28
00:02:16,010 --> 00:02:21,440
So the reason that that is a problem.

29
00:02:22,340 --> 00:02:31,350
Is that the stat region of the memory is where this local variable is being created, it is stored there.

30
00:02:31,370 --> 00:02:36,920
You remember in that lecture when we were talking about the virtual address space, the most recent

31
00:02:37,160 --> 00:02:43,670
virtual address space lecture where we showed the stack growing downwards towards lower memory addresses

32
00:02:43,670 --> 00:02:47,980
and we had those green boxes right, and each one was a stack frame.

33
00:02:48,710 --> 00:02:55,640
Whenever a function gets called, it makes a new stack frame and those frames pile up like plates,

34
00:02:55,640 --> 00:02:59,900
you know, stacks of plates growing towards lower memory and the stack is growing down.

35
00:03:01,790 --> 00:03:06,620
Well, there was a stack frame for this, my other function, when it gets cold, it has a new stack

36
00:03:06,620 --> 00:03:06,920
frame.

37
00:03:06,920 --> 00:03:10,910
So what you can imagine is that there's all those houses on the street, right?

38
00:03:12,380 --> 00:03:18,560
Before a house is even built, there still is an address for the empty lot, right?

39
00:03:20,040 --> 00:03:27,170
So it's it's kind of like there was an empty lot and, you know, maybe it had some other stuff there

40
00:03:27,180 --> 00:03:31,350
you don't even know, maybe there was another house there, but what happened was got rid of one house,

41
00:03:31,350 --> 00:03:36,210
got rid of whatever it was there, bulldozed it, built a new house for this function, right?

42
00:03:36,390 --> 00:03:39,900
New house went ahead and stored some things in that house.

43
00:03:39,900 --> 00:03:41,130
Maybe, you know, there's like.

44
00:03:43,370 --> 00:03:44,150
You stored.

45
00:03:44,240 --> 00:03:49,990
I don't know, there's like a car in the garage, and that is this integer, right?

46
00:03:50,000 --> 00:03:52,160
It's a local piece of data.

47
00:03:52,160 --> 00:03:53,180
It's a local variable.

48
00:03:53,180 --> 00:03:55,730
It's stored inside that house in a stack frame.

49
00:03:56,570 --> 00:04:05,090
When the function is done, that stack frame, there's a good chance that stack frame gets torn down

50
00:04:05,450 --> 00:04:08,690
because the operating system is managing that memory.

51
00:04:09,200 --> 00:04:10,670
It does a lot of stuff, right?

52
00:04:11,330 --> 00:04:13,040
It's just a complicated zone.

53
00:04:13,040 --> 00:04:18,620
It's like a really busy street, which is houses going up and going down and just cars flying in and

54
00:04:18,620 --> 00:04:22,090
out like all over the place, data going everywhere.

55
00:04:22,100 --> 00:04:27,350
It's a real, messy zone in the operating system has like a lot of different rules that it uses to manage

56
00:04:27,350 --> 00:04:28,010
that area.

57
00:04:29,630 --> 00:04:35,270
So what that means is that you should always consider when the function is done.

58
00:04:36,220 --> 00:04:45,070
You don't necessarily have access to this exact variable in memory anymore because it was created in

59
00:04:45,070 --> 00:04:46,450
that stack frame.

60
00:04:46,690 --> 00:04:52,480
And now that stack frame is not going to be the same way it was when you were inside this function.

61
00:04:53,650 --> 00:04:59,890
So when the execution is flowing through this function, your program is being ran in, it's done with

62
00:04:59,890 --> 00:05:00,670
that function.

63
00:05:01,090 --> 00:05:03,520
This might just disappear.

64
00:05:04,270 --> 00:05:06,690
It might still be there or might still disappear.

65
00:05:06,700 --> 00:05:12,370
You don't know that's up to the operating system to decide, but the thing is that you shouldn't rely

66
00:05:12,370 --> 00:05:12,790
on it.

67
00:05:12,970 --> 00:05:17,350
And so what happens when we want to use this?

68
00:05:17,350 --> 00:05:20,980
So it returns the address here that goes right here, right?

69
00:05:20,980 --> 00:05:26,650
Because we called this function and it got returns down to here this address of local VAR.

70
00:05:27,950 --> 00:05:32,630
That address then got saved into this point, and now we have a pointer with that address and then what

71
00:05:32,630 --> 00:05:33,920
do we try and do with it?

72
00:05:33,930 --> 00:05:37,430
Maybe we try and print it out, right?

73
00:05:38,660 --> 00:05:40,400
Main main point here.

74
00:05:40,760 --> 00:05:50,990
Print out, main pointer in line, you know, and then maybe we do something like, you know, main

75
00:05:50,990 --> 00:05:57,530
pointer equals 50 or 540, I don't know.

76
00:05:57,530 --> 00:06:00,770
And then we go ahead and print out mean pointer again now.

77
00:06:04,490 --> 00:06:06,230
So let me go ahead and save this.

78
00:06:07,640 --> 00:06:10,490
And then run this hour compiled and one, compile it.

79
00:06:11,540 --> 00:06:14,030
And then what happens when we compile it so.

80
00:06:15,150 --> 00:06:16,140
Let's check this out.

81
00:06:17,680 --> 00:06:26,500
So what it does is it doesn't give us in error, but it gives us a warning and it's giving us a warning

82
00:06:26,500 --> 00:06:28,600
about doing this right here.

83
00:06:30,430 --> 00:06:35,290
So it says address of local variable returned.

84
00:06:35,830 --> 00:06:42,760
So it's talking about Line 10, this local variable, which referring to my 10, but it knows that,

85
00:06:42,760 --> 00:06:44,590
you know, that's going to be a problem.

86
00:06:44,710 --> 00:06:49,050
But like I said, C++ is going to go ahead and let you do it.

87
00:06:49,060 --> 00:06:50,230
This is just a warning.

88
00:06:50,230 --> 00:06:51,100
It's not an error.

89
00:06:51,370 --> 00:07:00,430
This program still compiled successfully so we can still run this executable, even though this is a

90
00:07:00,430 --> 00:07:01,490
problem, you know?

91
00:07:01,990 --> 00:07:05,470
This may or may not exist when we run it, this function.

92
00:07:06,790 --> 00:07:10,840
When it's getting ran, you know, all this stuff will be in a stacked frame and we get torn down,

93
00:07:10,840 --> 00:07:15,730
and then we're thinking that we still have access to this zone of memory expecting us to be able to

94
00:07:15,730 --> 00:07:16,330
print it out.

95
00:07:17,350 --> 00:07:19,990
So let's go ahead and show that we can run this.

96
00:07:22,230 --> 00:07:22,930
I run it.

97
00:07:22,950 --> 00:07:24,930
Nothing prints out, right?

98
00:07:25,170 --> 00:07:27,270
And this might not be the case for you.

99
00:07:27,510 --> 00:07:28,860
Something might print out.

100
00:07:29,100 --> 00:07:30,600
You might see some numbers.

101
00:07:30,990 --> 00:07:32,130
I don't really know.

102
00:07:32,580 --> 00:07:34,770
Like I said, it's undefined behavior.

103
00:07:34,770 --> 00:07:39,420
So what should we do if this is bad?

104
00:07:40,140 --> 00:07:45,990
Then what can we do if we want to make some sort of local variable in here in this function?

105
00:07:46,560 --> 00:07:48,440
But we want to be able to return it.

106
00:07:48,450 --> 00:07:49,380
What would we do?

107
00:07:49,830 --> 00:07:54,870
Well, like I said, the heap is a much less busy area, right?

108
00:07:54,900 --> 00:07:58,380
It's not managed quite as much by the operating system.

109
00:07:58,380 --> 00:07:59,580
It's a little more free.

110
00:08:00,060 --> 00:08:06,650
So what we could do is return that value instead on a heap, right?

111
00:08:06,660 --> 00:08:09,600
We can put this on the heap instead of putting it on the stack.

112
00:08:11,830 --> 00:08:18,250
So what I could do is comment this out and kind of do a similar thing, but make it on the heap so I

113
00:08:18,250 --> 00:08:22,540
could say and and then make a pointer and I could just say.

114
00:08:26,150 --> 00:08:37,540
You know, I'll just call this local bar and I can say equals new and like this, and then I could say,

115
00:08:37,540 --> 00:08:42,700
you know, a local bar, star, local bar I.D. reference ID, I say it's five.

116
00:08:43,480 --> 00:08:49,450
And then what I could do is I could just return local bar because it's already a pointer, right?

117
00:08:50,470 --> 00:08:52,000
So I could do something like this.

118
00:08:52,510 --> 00:08:55,120
I would like to also show you some new syntax right here.

119
00:08:55,120 --> 00:09:01,000
So if you don't want to do something like this, you can all on one line make it initialized to five.

120
00:09:01,510 --> 00:09:04,330
By doing that with putting some parentheses there.

121
00:09:06,010 --> 00:09:11,770
So instead of doing this extra line if you want, you can both declare this variable and then do the

122
00:09:11,770 --> 00:09:17,530
new int and initialize it with the value five by putting in parentheses after the end.

123
00:09:20,010 --> 00:09:25,170
So this changes stuff up, this will make it OK and won't give a warning for us, so let's go ahead

124
00:09:25,170 --> 00:09:26,130
and compile this.

125
00:09:26,640 --> 00:09:30,270
We notice we don't get any warning here and then let's go ahead and run it.

126
00:09:30,930 --> 00:09:33,690
You notice it prints out what we expected this time.

127
00:09:34,170 --> 00:09:38,420
We initialized it with five in here and we went ahead and returned this local bar.

128
00:09:38,890 --> 00:09:44,060
We got the pointer and got saved in this pointer, right?

129
00:09:44,100 --> 00:09:46,950
As we returned an integer pointer, it got saved in this pointer.

130
00:09:47,550 --> 00:09:53,610
Remember that when we had an ampersand here before we were returning an address, but also remember

131
00:09:53,610 --> 00:09:55,230
that pointers are just addresses.

132
00:09:55,260 --> 00:10:00,120
That's what I said in the last tutorial, the last video I was just talking about the fact that it's

133
00:10:00,120 --> 00:10:01,350
really just doing an address.

134
00:10:01,980 --> 00:10:06,120
Now we're doing the same thing, but it's just we're returning explicitly the pointer, right?

135
00:10:06,300 --> 00:10:07,140
We made a pointer.

136
00:10:07,140 --> 00:10:08,280
We return a pointer.

137
00:10:08,520 --> 00:10:10,890
The pointer is getting saved into this other pointer.

138
00:10:10,890 --> 00:10:12,210
They're all the same data types.

139
00:10:12,210 --> 00:10:13,500
So this is all gravy.

140
00:10:13,500 --> 00:10:14,130
It's OK.

141
00:10:14,730 --> 00:10:16,410
We go ahead and we print that out.

142
00:10:16,410 --> 00:10:17,550
It was five, right?

143
00:10:17,550 --> 00:10:20,070
So it's printing out what it was initialized to here.

144
00:10:20,070 --> 00:10:21,300
We d reference it.

145
00:10:21,660 --> 00:10:23,430
We change it to five hundred forty.

146
00:10:23,700 --> 00:10:27,120
Then we print it out and we see this expected result here.

147
00:10:28,500 --> 00:10:29,670
Why is this OK?

148
00:10:29,790 --> 00:10:31,740
Well, it's because it's on the heap.

149
00:10:32,040 --> 00:10:33,960
The heap is a lot more free.

150
00:10:34,230 --> 00:10:34,800
It's a lot.

151
00:10:34,950 --> 00:10:42,210
I would call it a more chill area, I guess, to be able to go ahead and do that and declare some variables

152
00:10:42,210 --> 00:10:46,610
there without us having the problem of, like, I don't know, Stack Frame got torn down.

153
00:10:46,620 --> 00:10:49,560
Who knows whether we can access this local variable anymore?

154
00:10:50,550 --> 00:10:50,910
All right.

155
00:10:50,910 --> 00:10:58,260
So an important point to know about using functions, returning addresses, making local variables.

156
00:10:58,770 --> 00:11:02,520
C++ sometimes lets you do things that you really shouldn't be doing.

157
00:11:03,220 --> 00:11:06,060
I just want to let you know that and point that out.

158
00:11:07,020 --> 00:11:09,620
Another thing that we need to do is what well, we did.

159
00:11:09,840 --> 00:11:13,350
We allocated this memory right?

160
00:11:14,750 --> 00:11:16,100
And you may be thinking well.

161
00:11:17,300 --> 00:11:20,130
It's in a local function, how do we allocate it?

162
00:11:20,150 --> 00:11:21,320
We don't know where it is.

163
00:11:21,350 --> 00:11:22,250
No, it's fine.

164
00:11:22,250 --> 00:11:22,980
It's on the heap.

165
00:11:22,980 --> 00:11:25,670
We're returning a pointer, which is just an address.

166
00:11:26,240 --> 00:11:27,770
This address gets returned.

167
00:11:27,770 --> 00:11:33,560
We're still able to refer to that address, except that actual memory that's at that address.

168
00:11:34,460 --> 00:11:41,630
It's way over and he planned, and we still have access to that zone of memory so we can go ahead and

169
00:11:41,630 --> 00:11:47,600
do a delete and we can go ahead and delete main pointer.

170
00:11:49,290 --> 00:11:51,780
We didn't declare main point, right?

171
00:11:53,970 --> 00:11:56,670
That's not what we declared, we declared this pointer.

172
00:11:57,930 --> 00:12:04,500
And it was pointing to this piece of memory return, that pointer, that pointer got saved in here.

173
00:12:07,120 --> 00:12:08,200
We printed it out.

174
00:12:09,270 --> 00:12:14,280
And you may be thinking, well, we've done all this stuff with it, but why don't we have to delete

175
00:12:15,150 --> 00:12:16,650
local law, Poyner?

176
00:12:17,160 --> 00:12:18,690
Isn't that what we have to delete?

177
00:12:19,740 --> 00:12:26,340
These things main pointer and local law pointer, they're having the same address in it, and the thing

178
00:12:26,340 --> 00:12:33,030
that really matters to delete is what's at that address, so we can still follow main pointer to the

179
00:12:33,030 --> 00:12:37,650
same address as local VAR, right?

180
00:12:38,430 --> 00:12:42,690
And that's going to contain they're both pointing to the same region of memory.

181
00:12:42,720 --> 00:12:46,050
So here, when we delete main pointer, we still solve that problem.

182
00:12:46,050 --> 00:12:50,790
We still deleted the memory, the memories, the thing that we need to free up, right?

183
00:12:50,790 --> 00:12:52,260
We need to unreservedly.

184
00:12:53,130 --> 00:12:54,990
Right, so that can be used still.

185
00:12:55,470 --> 00:12:58,010
So this is totally valid and fine to do.

186
00:13:00,350 --> 00:13:00,890
The.

187
00:13:01,890 --> 00:13:07,080
Local stuff in the function, like the address, you know, it can be created on the stack, but it's

188
00:13:07,080 --> 00:13:11,970
really just a pointer that memory that we're referring to is actually over on the heap, right?

189
00:13:12,960 --> 00:13:18,300
So just want to like really make sure that you understand the different locations that data is going

190
00:13:18,300 --> 00:13:24,240
into, you know, we here, we're storing some addresses somewhere in those addresses point to other

191
00:13:24,240 --> 00:13:27,060
regions of memory that store the actual data that we care about.

192
00:13:27,570 --> 00:13:27,930
Right.

193
00:13:27,930 --> 00:13:30,870
And since we did it this way, we didn't run into any warning.

194
00:13:30,870 --> 00:13:32,640
So we don't have issues, right?

195
00:13:34,310 --> 00:13:38,780
OK, so this is kind of a complex thing to wrap your head around, like I said, I'm breaking these

196
00:13:38,780 --> 00:13:46,970
into kind of smaller bite sized lectures, so it's easier to, I guess, digest or ingest whatever you

197
00:13:47,540 --> 00:13:51,590
will, you know, but I'm going to go ahead and in that lecture here.

198
00:13:52,130 --> 00:13:57,710
So the next lecture, I can start up on another topic and I don't cluster too much complicated stuff

199
00:13:57,710 --> 00:13:58,280
together.

200
00:13:58,970 --> 00:13:59,240
All right.

201
00:13:59,250 --> 00:14:01,340
So with that, I will see you in the next lecture.
