WEBVTT

00:00.900 --> 00:03.910
So teachers can be quite dangerous.

00:03.990 --> 00:09.690
So you do need to use them properly and really think about what you're doing.

00:10.470 --> 00:16.110
Every every single time when you use one and the reasons why using one.

00:16.110 --> 00:30.420
So you saw before that if you had a pointer but say this pointer and you set it to null pointer you

00:30.420 --> 00:32.170
can't dereferences right

00:35.560 --> 00:48.100
because it doesn't know it doesn't know what my pointer is at this point because no no pointer is not

00:48.100 --> 00:49.560
a type.

00:49.670 --> 00:52.420
This is not it it's not types doing something.

00:52.450 --> 00:54.200
This is very bad.

00:54.600 --> 00:55.170
OK.

00:55.480 --> 00:57.330
So that's kind of one pitfall.

00:57.340 --> 00:58.670
There are others though.

00:58.930 --> 01:12.990
So let's see a different example so say we want to return a pointer so like this in which we just kind

01:12.990 --> 01:16.390
of make it right here.

01:19.090 --> 01:23.530
So we say it's just five

01:26.420 --> 01:38.110
or turn the value of x and in let's just say it's pointer equals to phunk

01:41.930 --> 01:52.550
and we're going to do that's value pointed to by X..

01:52.620 --> 01:58.120
Winter is next winter.

02:01.180 --> 02:05.240
You got.

02:06.360 --> 02:10.410
And if try this.

02:10.470 --> 02:12.940
So what's happening here.

02:12.960 --> 02:15.440
Values one that's kind of strange.

02:15.480 --> 02:20.520
So remember what I said about lifetime here.

02:20.730 --> 02:26.050
So X will be destroyed at the end of the function.

02:26.130 --> 02:36.090
So if you remember the lifetime lecture this only has a lifetime of this function so it's destroyed

02:36.090 --> 02:39.600
as soon as it returns.

02:40.020 --> 02:46.970
So therefore returning an address that has been destroyed doesn't shouldn't exist anymore.

02:47.040 --> 02:48.870
At the end of this function.

02:49.200 --> 02:55.260
So the moral of the story here is you should never return an address of a local variable out of its

02:55.260 --> 02:57.100
function.

02:57.540 --> 03:01.500
Of course it's the same if you use a reference.

03:02.550 --> 03:07.110
So in a similar way you shouldn't do something like this.

03:09.250 --> 03:12.920
We had a pointer here.

03:13.080 --> 03:13.680
It's

03:16.340 --> 03:25.020
changes to be it's X so what changes FOID

03:31.480 --> 03:36.680
and you should do something like this.

03:36.830 --> 03:37.660
Say it.

03:37.660 --> 03:38.700
Why.

03:40.090 --> 03:54.430
Ten inch x 2 that can do this and it's set.

03:54.500 --> 04:07.970
Ch ch x 5 year an x K.

04:12.670 --> 04:21.030
You X is.

04:21.320 --> 04:22.350
So this

04:26.050 --> 04:35.150
so the value of x is 5 even though we supposedly changed it right.

04:35.500 --> 04:41.540
Well this is it is not quite the same.

04:41.540 --> 04:44.070
This doesn't do what you think it does.

04:44.190 --> 04:52.280
K because this pointer will be destroyed by the end of this function.

04:52.490 --> 04:55.040
Similar to the last example.

04:55.040 --> 05:02.870
So you're kind of giving it a an address that will be destroyed and itself will be destroyed at the

05:02.870 --> 05:08.060
end of the function.

05:08.810 --> 05:12.810
So it doesn't really change X at all here.

05:13.680 --> 05:24.390
So we're seeing some more pitfalls that we might run into a bit later when we get into some more more

05:24.390 --> 05:25.480
new stuff.
