WEBVTT

00:00.270 --> 00:07.800
I want to show you some of the pitfalls that you should avoid when using dynamic memory.

00:07.830 --> 00:10.350
So here's a simple one.

00:10.540 --> 00:20.710
Say add and pointer and it points to this new integer and we say each winter this this is OK.

00:22.180 --> 00:27.310
Then you say lead again.

00:27.800 --> 00:32.250
So double delete a pointer to memory.

00:32.480 --> 00:36.980
Remember that once you delete the memory the memory is gone back to the heap.

00:36.980 --> 00:42.770
So your program doesn't really control it anymore and it's part of the heap now.

00:42.980 --> 00:47.830
So once you delete it again really only bad things will happen.

00:48.630 --> 00:59.840
So instead what you should do is always say this to set it to null pointer and so this is good.

01:01.300 --> 01:16.700
So Never leave something in another example something to not do is say we had pointer of 10 integers

01:16.790 --> 01:18.180
and it doesn't have to be.

01:18.180 --> 01:23.460
Could could still we could do it this way too but it's going to do it for a difference.

01:23.870 --> 01:32.690
And let's say we had a different integer an actual integer set to five year.

01:32.730 --> 01:41.050
So this point your points to the head of these 10 contiguous integers in memory.

01:41.050 --> 01:41.420
Right.

01:41.470 --> 01:45.440
So this array of 10 integers sets went to the head.

01:45.450 --> 01:46.740
But what if you did this.

01:46.770 --> 01:50.390
So point it to something else.

01:50.400 --> 01:51.240
OK.

01:51.600 --> 01:54.330
So this is very bad.

01:54.420 --> 01:55.040
OK.

01:55.350 --> 02:04.770
This is what's called a memory leak because there's no way to reclaim where it regionally pointed to.

02:04.850 --> 02:05.840
Right.

02:05.960 --> 02:11.560
Like we pointed originally to the head of some memory which gave some address back right.

02:11.870 --> 02:16.090
But now we're pointing somewhere else and our owning pointer.

02:16.250 --> 02:18.830
Originally this was the owning pointer.

02:19.010 --> 02:22.750
Right now kind of points somewhere else.

02:24.520 --> 02:28.410
Which is very bad so never ever ever do this.

02:28.750 --> 02:29.750
OK.

02:29.830 --> 02:32.950
So in a similar way

02:35.650 --> 02:40.030
here can say something like this.

02:43.340 --> 02:48.450
Just put the address

02:51.710 --> 02:52.460
winter

02:55.830 --> 02:59.360
this.

02:59.620 --> 03:04.470
And then remember we could do this you could increment the pointer right.

03:04.870 --> 03:10.810
And that would give you the address of the next one.

03:10.900 --> 03:17.310
The next item in the array right.

03:17.360 --> 03:23.610
So if we did that and ran it of course you'd get.

03:24.090 --> 03:26.910
You know it's four bytes more.

03:26.920 --> 03:28.200
Right.

03:28.300 --> 03:38.250
So the next one which is fine but what you've really done here is now you don't point to the head anymore.

03:38.510 --> 03:45.160
OK point to the initial address.

03:45.390 --> 03:45.770
Right.

03:45.810 --> 03:48.680
So you're kind of pointing to the next one instead.

03:49.410 --> 04:01.790
OK so you should never you should always leave the initial pointer owning pointer alone and access it

04:01.790 --> 04:07.630
through some other means like making a new pointer and pointing it to the memory.

04:07.650 --> 04:17.500
So any owning pointer should always point exactly to where pointed to when you knew the memory.

04:17.510 --> 04:18.130
OK.

04:18.910 --> 04:22.140
And it would be even worse if you did something like this.

04:22.850 --> 04:23.370
This would have.

04:23.370 --> 04:27.320
Now you said delete point here and you kind of forgot that you incremented it.

04:27.540 --> 04:29.040
Well see what happens.

04:31.640 --> 04:33.240
Oh not good.

04:33.240 --> 04:33.820
Right.

04:34.040 --> 04:44.160
So it says pointer being freed it's say was not allocated.

04:44.160 --> 04:47.580
It set a breakpoint and all this all this stuff right.

04:47.580 --> 04:51.220
So it's really saying hey you're deleting something.

04:51.340 --> 04:53.110
In fact we even do it wrong.

04:53.130 --> 04:53.460
Right.

04:53.460 --> 04:54.280
It should be like this.

04:54.280 --> 04:59.890
Even OK you're deleting something but it wasn't.

04:59.890 --> 05:03.380
It's not to the same address as it should be.

05:03.390 --> 05:03.780
Right.

05:03.790 --> 05:07.510
It's not pointing to the original addresses really what this is saying.

05:07.920 --> 05:08.530
OK.

05:08.680 --> 05:19.120
So this is very very bad to do so always just keep the same or if it's an owning pointer just keep it

05:19.270 --> 05:22.400
pointing to where it originally pointed to.

05:22.690 --> 05:23.140
OK.

05:23.140 --> 05:25.850
Access it through some other pointer.

05:25.900 --> 05:30.010
So if you really wanted to do this we should say.

05:30.100 --> 05:32.260
Pointer to right.

05:33.780 --> 05:36.590
Hey goes to pointer.

05:37.170 --> 05:42.710
And then we use pointer to where here.

05:43.860 --> 05:46.230
Are two parts plus K.

05:46.400 --> 05:57.720
And then we could delete the memory OK of the owning pointer of your and this should be OK.

05:58.000 --> 06:06.960
Jay so always access it through a different pointer and keep the owning pointer the same

06:11.770 --> 06:16.750
so other pitfalls again deleting pointers you don't own right.

06:16.780 --> 06:18.140
So never do that.

06:18.190 --> 06:22.940
Not assigning the null pointer after the memory was ready.

06:23.260 --> 06:29.070
Here's a nother example let's say you had

06:34.940 --> 06:40.340
her course new integer 10

06:42.690 --> 06:47.580
integers and never do this.

06:47.590 --> 06:49.830
OK you're here sort of.

06:50.190 --> 06:56.190
This is the same as with the arrays you're kind of are assigning.

06:56.190 --> 06:59.190
Say so you're signing outside

07:01.800 --> 07:04.670
your index range right.

07:05.760 --> 07:09.230
Ok it's very bad to do this.

07:09.270 --> 07:11.160
We don't know what will happen.

07:12.100 --> 07:15.670
If you do this it might crash it might not crash.

07:15.670 --> 07:20.070
Hopefully it will crash because at least then you'll see where is crashing.

07:20.090 --> 07:20.950
Right.

07:21.190 --> 07:24.000
So that that would this is bad to do.

07:26.880 --> 07:41.520
And of course if you do something like this say it and then you delete that with the array image mismatch

07:41.560 --> 07:42.100
right.

07:42.790 --> 07:44.360
Her mismatch.

07:44.740 --> 07:51.430
So you knew that with only the regular new but you're deleting it with the array delete.

07:51.490 --> 07:53.350
So don't do this.

07:53.350 --> 07:55.600
This is also very bad.

07:55.660 --> 07:57.220
And of course vice versa.

07:57.220 --> 07:57.700
Right.

07:59.170 --> 08:07.350
So these are some of the pitfalls if you follow the guidelines outlined by this time you should.

08:07.340 --> 08:09.110
You should usually be OK.

08:09.190 --> 08:19.380
I tend not to use dynamic memory so much but it's useful in some search situations and in the next section

08:20.370 --> 08:25.280
when we do our hangman game you'll see just how useful it can be.
