WEBVTT

00:00.700 --> 00:07.010
You can create more than just one variable using new he can create an array as well.

00:07.290 --> 00:09.580
Let's see how you do it.

00:09.800 --> 00:13.440
Say we had it's pointer.

00:13.730 --> 00:24.450
This and we say new inch in the square brackets and the number of are the length of the array.

00:24.810 --> 00:27.850
OK here using those array brackets again.

00:28.080 --> 00:36.770
And in those brackets we're creating 10 new integers in a contiguous array so the 10 doesn't have to

00:36.770 --> 00:39.850
be a constant or literal value.

00:39.860 --> 00:44.240
It can be any integer created by any means.

00:44.310 --> 00:56.450
So could have something say a length and you actually get it from the user say so please.

00:56.690 --> 00:57.140
Like

01:01.140 --> 01:02.230
get the link.

01:04.110 --> 01:06.410
This and we use it here.

01:09.600 --> 01:17.600
K so it doesn't have to be a constant value could be any integer here.

01:17.770 --> 01:26.580
And this is why dynamic memory is so flexible it can create how many you want using this new keyword

01:29.580 --> 01:37.440
and this is really the reason why pointers kind of act like arrays because it needs to be able to have

01:37.440 --> 01:42.030
the ability to access all of those memory elements that it points to.

01:42.300 --> 01:50.700
So initially this line will point to the very first memory address cancer points it kind of ahead of

01:50.700 --> 01:58.860
the memory but we can use this like any other normal array that we could access through a pointer.

01:58.860 --> 02:02.160
So it's an example.

02:02.570 --> 02:06.320
It's just set the array

02:10.010 --> 02:14.080
to say so.

02:14.100 --> 02:21.420
And just like any other one like this it would set it to I'm plus one let's say.

02:21.920 --> 02:26.450
And we actually just put it all over

02:30.980 --> 02:39.900
plus to say in each here.

02:44.500 --> 02:45.370
It's

02:53.960 --> 03:03.370
OK so if we run this inter length let's say 20 and it prints out 20.

03:03.650 --> 03:04.150
OK.

03:04.230 --> 03:10.560
All 20 of these out as they see the indices go from zero to 19.

03:10.560 --> 03:11.360
Right.

03:11.470 --> 03:12.900
It's like a normal array.

03:13.050 --> 03:13.570
OK.

03:15.230 --> 03:21.110
So once you're done with that memory we should delete it like you said last time.

03:21.110 --> 03:31.370
So instead of just saying delete pointer like we had four we actually have to say delete array pointer

03:32.510 --> 03:33.890
OK.

03:36.110 --> 03:37.300
Pointer here.

03:38.620 --> 03:39.020
OK.

03:39.210 --> 03:51.600
So when you make a new array like we do up here after you delete it using this kind of array delete

03:51.630 --> 03:53.500
array syntax.

03:53.940 --> 03:57.750
So we can just say delete in point int pointer like this.

03:57.750 --> 04:02.620
Because that would only delete the very first memory address.

04:02.670 --> 04:03.730
OK.

04:04.500 --> 04:14.840
Which is wrong so you have to use the array delete if you if you knew that with the array new.

04:15.070 --> 04:15.630
OK.

04:16.640 --> 04:25.350
So in the next lecture I want to go over some best practices for using dynamic memory.
