WEBVTT

00:04.570 --> 00:11.800
In this lecture I want to talk about some guidelines on how to use dynamic memory properly and that's

00:11.810 --> 00:15.780
really just my guidelines that I've developed over the years.

00:15.910 --> 00:22.960
And you once you have some experience you can decide for yourself if you want to keep using them or

00:22.960 --> 00:24.900
develop your own.

00:24.910 --> 00:32.140
So one rule I like to follow is that whoever made the memory should be the one responsible to delete

00:32.140 --> 00:32.760
it.

00:33.190 --> 00:35.950
So let me walk you through what I've done here.

00:35.950 --> 00:38.080
So we have two functions.

00:38.080 --> 00:45.100
So this first function right here returns a dynamically created integer.

00:45.310 --> 00:50.010
And this next function just uses the integer.

00:50.370 --> 00:53.930
You'll see I use funny names of Schultes plain in a second.

00:54.370 --> 01:09.970
So in here Maine is issuing the call to create a new integer using this function func right here.

01:12.000 --> 01:20.270
And it assumes that it knows that func actually does create a new integer doesn't just pass you back

01:20.280 --> 01:23.300
pointer of some local variable.

01:23.370 --> 01:31.290
Ok So ultimately then it's up to Maine to delete the pointer once it's done with it.

01:31.380 --> 01:37.410
OK so that's why you'll see down here I say delete the pointer

01:40.450 --> 01:46.650
and this is the concept of memory ownership thing.

01:46.670 --> 01:55.130
I would add to this is to name the variables in such a way that it indicates who owns what.

01:55.130 --> 01:58.010
So that's kind of what I've done here.

01:58.010 --> 02:09.010
Usually when I have a pointer that actually owns some memory all kind of put a prefix on the name and

02:09.020 --> 02:11.910
actually say oh pointer.

02:12.260 --> 02:14.710
So this stands for owning pointer.

02:14.720 --> 02:20.970
So it owns the memory pointed to by this pointer is what this really sets.

02:20.990 --> 02:21.520
OK.

02:21.860 --> 02:33.690
So it takes ownership of the memory and you'll see down here and this use all have this kind of no pointer.

02:34.190 --> 02:37.820
Well this stands for non owning pointer.

02:38.080 --> 02:38.660
OK.

02:38.780 --> 02:40.070
So this.

02:40.280 --> 02:49.970
So this guy right here is use function can use the pointer however it likes but it knows not to delete

02:49.970 --> 02:50.480
it.

02:50.480 --> 02:52.550
It's not its responsibility.

02:52.670 --> 02:55.450
OK so that's what no pointer.

02:56.000 --> 02:59.240
That's my convention for the name of it.

02:59.340 --> 03:07.070
OK so it's very important to be able to read the code and know who owns was.

03:07.500 --> 03:14.370
So like I said if I pass a pointer to a function that I know I didn't own the pointer then I put the

03:14.370 --> 03:16.640
no pointer in front of it.

03:16.670 --> 03:24.270
OK so I find this is a very good way to remember who owns what and who's responsible for deleting the

03:24.270 --> 03:24.920
memory.

03:26.010 --> 03:33.720
And once you delete the pointer you should always set it to null pointer after.

03:34.030 --> 03:42.480
And this is because really you're still pointing to the same memory address even after it gets deleted.

03:42.480 --> 03:50.760
But since it really doesn't since is it's not really accessible by you anymore at least it shouldn't

03:50.760 --> 03:54.080
be you shouldn't keep a reference to it.

03:55.110 --> 03:58.420
And so that's kind of the reason why.

03:58.520 --> 04:04.960
So if I did anything with the pointer after we delete it it's completely undefined behavior.

04:04.970 --> 04:10.980
So we don't know what's going to happen if I use it in any way.

04:11.060 --> 04:16.490
And so that's why I put the null pointer here because if I do use it and Id reference it Liesel crash

04:16.490 --> 04:19.560
in L.A. where I crashed.

04:21.110 --> 04:23.190
So it's called the dangling pointer.

04:23.210 --> 04:25.750
I don't set you know pointer.

04:26.330 --> 04:30.560
And these are really the primary guidelines that I use.

04:30.560 --> 04:33.590
In fact we have too many others.

04:33.590 --> 04:41.600
It's really just make sure I know who points to what and who owns what and if they don't own it then

04:41.630 --> 04:44.760
I should say that they don't own it.

04:45.410 --> 04:47.100
And one last thing here.

04:47.150 --> 04:58.720
You saw the example before where so Ulos in this function and I return a pointer OK.

04:59.030 --> 05:03.390
And we saw an example before where I said something like this.

05:05.890 --> 05:07.340
Add A.

05:07.540 --> 05:15.540
X equals 5 and then I kind of return the return the address of the X..

05:15.810 --> 05:22.200
Well this is wrong because we know that X will be destroyed by the end of the function.

05:22.370 --> 05:30.350
But here since I'm returning a new int i know that it won't be destroyed because it's comes from the

05:30.350 --> 05:38.390
heap and we as per armorers have to delete it not and it's not automatic like it would be if it was

05:38.390 --> 05:39.850
a local variable.

05:39.920 --> 05:42.800
And so that's why doing this is OK.

05:42.880 --> 05:52.190
This is totally fine and it's OK to do so I just want to go over it in the next lecture I want to talk

05:52.190 --> 05:59.810
about the pitfalls of not being careful when using dynamic memory.
