WEBVTT

00:01.340 --> 00:04.660
Memory and the C plus plus standard library here.

00:04.670 --> 00:11.960
This C plus plus standard library provides various classes to allow you manipulate collection of objects.

00:11.990 --> 00:18.680
These classes called the STL Standard Template Library.

00:18.680 --> 00:26.990
So this provides a standard way to insert items into collection objects and ways to access the items

00:26.990 --> 00:29.720
and iterate through the entire collections.

00:29.720 --> 00:31.460
This is called the Iterators.

00:31.460 --> 00:41.090
Here, the STL defines collections classes that are implemented as stacks or as a vectors with a random

00:41.090 --> 00:41.540
access.

00:41.540 --> 00:45.560
Here, these classes will be covered in depth in the next lecture.

00:45.560 --> 00:53.780
So in this section we will limit the discussion to just two classes that behave like C plus plus built

00:53.780 --> 00:54.740
in arrays.

00:54.770 --> 00:58.850
The first, let's begin with the standard library arrays here.

00:58.880 --> 01:09.620
The standard library arrays provides two containers that given random access via an indexer to the data.

01:09.620 --> 01:17.000
So these two containers also allows you to access the underlying memory, since they guarantee to store

01:17.000 --> 01:20.420
the items sequentially and contiguous in memory.

01:20.420 --> 01:24.800
And they can be used when you are required to provide a pointer to a buffer.

01:24.800 --> 01:32.330
So these two types are both templates, which means that you can use them to hold built in in the custom

01:32.330 --> 01:32.810
types.

01:32.810 --> 01:39.650
So these two collection classes are array and vector here.

01:41.950 --> 01:42.250
Yeah.

01:42.520 --> 01:43.960
So these are.

01:45.090 --> 01:48.360
These are the Wilton standard.

01:49.270 --> 01:50.290
Library.

01:51.230 --> 01:51.860
All right.

01:52.340 --> 01:57.020
So we can also use the stack based array class in C plus plus.

01:57.140 --> 02:05.360
Here the array class is defined in the array here, like that array header file here.

02:05.780 --> 02:12.680
So the class allows you to create fixed size arrays on the stack and with the built in arrays, they

02:12.680 --> 02:16.940
cannot shrink or expand at runtime.

02:16.940 --> 02:23.720
So since they are allocated on the stack, they do not require a call to a memory allocator at runtime.

02:23.720 --> 02:27.260
But clearly they should be smaller than the stack frame size.

02:27.260 --> 02:35.510
So this means that an array is a good choice for small array of items, so the size of an array must

02:35.510 --> 02:43.040
be known at compile time and it is passed as a template parameter like that.

02:43.040 --> 02:47.600
Here, for example, let's include a include array here.

02:50.010 --> 02:53.460
Ouray and Ouray.

02:54.530 --> 02:55.210
Here.

02:55.220 --> 02:57.920
Integer four and here.

02:57.920 --> 02:59.780
One, two, three, four.

03:00.790 --> 03:08.530
So in this code, the first template parameter in the angled braces is a type of each item in the array.

03:08.530 --> 03:12.520
And the second parameter is the number of items.

03:12.520 --> 03:21.670
So here number of items and the type of the each item in the array here.

03:21.670 --> 03:28.990
So this code initializes the array with an initialized list, but note that you still have to provide

03:28.990 --> 03:36.280
the size of an array in the template here and this object will work like a built in array or indeed

03:36.280 --> 03:40.450
any of the standard library containers with the ranged loop here.

03:40.450 --> 03:42.490
So let's create a range loop here.

03:42.790 --> 03:47.470
Integer E and here we created the array here.

03:47.470 --> 03:49.150
Make it here.

03:52.640 --> 03:57.870
Right here and we're going to create an array integer.

04:08.210 --> 04:08.510
Here.

04:10.360 --> 04:13.430
Firstly, we're going to create the array like that.

04:19.000 --> 04:22.750
Here and we're going to pass iterate through this array here.

04:26.310 --> 04:31.410
And each time we're going to print this on the screen.

04:34.300 --> 04:38.410
We have to import and use the namespace here.

04:40.460 --> 04:42.830
And print it out.

04:43.810 --> 04:47.080
As you can see, we printed our variables here.

04:47.110 --> 04:53.800
The reason is that our array implements the begin and end function that are required for this syntax.

04:53.800 --> 04:58.840
So you can also use this like that here, for example.

04:58.840 --> 05:16.450
INT for integer E zero oops, integer e equals zero, while e is less than r dot size here and plus

05:16.450 --> 05:20.900
plus E here and C out here.

05:21.220 --> 05:29.410
Our array here, our error E and n line here.

05:29.410 --> 05:32.230
We can write it also like that.

05:32.230 --> 05:33.940
It's the same output here.

05:33.970 --> 05:35.440
The size function here.

05:35.440 --> 05:41.740
As you can see here, we return the size of the array and the square braces.

05:41.740 --> 05:49.550
Indexer gives the random access to the members of the array so you can access the memory outside of

05:49.550 --> 05:51.220
the bounds of the array.

05:51.230 --> 06:00.170
So for the previously defined array that has four members you can access like this array r ten.

06:00.170 --> 06:05.360
So this may cause the unexpected behavior at runtime.

06:05.360 --> 06:06.410
Let's try it out.

06:07.300 --> 06:09.780
As you can see here, there's just a reprinted.

06:09.790 --> 06:11.260
There's a bunch of zeros.

06:12.060 --> 06:12.780
Here.

06:12.780 --> 06:19.720
So or this may cause unexpected behavior or even some kind of memory fault.

06:19.740 --> 06:27.960
So to guard against this, the class provides a function F which will perform, uh, perform a range

06:27.960 --> 06:28.340
check.

06:28.350 --> 06:37.830
And if the index is out of range, the class will throw the cplusplus exception, which, uh, here.

06:39.250 --> 06:39.580
Here.

06:40.920 --> 06:43.020
C plus plus exception named.

06:43.050 --> 06:45.390
Out of range here.

06:45.810 --> 06:49.020
So the main advantage of using an array.

06:49.410 --> 06:56.610
Array object is that you get compile time checks to see if you are inadvertently passing the object

06:56.610 --> 06:59.520
to a function as a dump pointer here.

06:59.910 --> 07:02.140
Let's make another example here.

07:02.160 --> 07:03.030
Void.

07:04.110 --> 07:04.500
Here.

07:05.960 --> 07:09.410
Void use an init.

07:10.180 --> 07:12.100
An integer pointer here.

07:12.400 --> 07:17.620
So at runtime the function does not know the size of the buffer passed to it.

07:17.620 --> 07:26.080
And in this case, the documentation says that you must pass a buffer with ten integer types variables.

07:26.080 --> 07:34.300
But as we see in C plus plus allows you allows a built in array to be used as a pointer.

07:34.300 --> 07:36.850
So we can do it like that.

07:37.210 --> 07:39.340
In practice use.

07:41.010 --> 07:41.730
Ten.

07:42.960 --> 07:43.830
Ah one.

07:43.830 --> 07:44.610
So.

07:45.510 --> 07:45.810
Oops.

07:49.500 --> 07:49.800
Here.

07:49.980 --> 07:55.410
Actually, let's create another array for this example and make comment out.

07:56.650 --> 07:58.540
Comment it in here.

07:59.440 --> 07:59.940
Okay.

07:59.950 --> 08:01.090
Integer array.

08:02.490 --> 08:02.880
Here.

08:02.880 --> 08:04.710
And one, two, three, four.

08:04.980 --> 08:07.650
This contains same variables as previous arrays.

08:08.040 --> 08:08.970
Here.

08:10.100 --> 08:17.050
And then this will read the past, the end of the buffer here.

08:17.060 --> 08:20.570
As you can see here, we got an exception.

08:20.570 --> 08:28.550
So this is no compiler checker nor any runtime check to um, catch this error.

08:28.760 --> 08:36.680
But there is the array class will not allow such an error to happen because there is no automatic conversion

08:36.680 --> 08:39.530
into a dump pointer here.

08:39.530 --> 08:48.320
So if you insist in obtaining a dump pointer, you can do this and be guaranteed to have access to data

08:48.860 --> 08:53.360
as a contiguous block of memory where the items stored sequentially.

08:53.720 --> 08:55.850
So you can do it like that.

08:59.460 --> 08:59.760
Here.

09:00.860 --> 09:01.880
Let's compile it.

09:04.250 --> 09:06.710
And, uh, here.

09:10.310 --> 09:14.690
We got Elder turned one Subcommand filed.

09:15.380 --> 09:20.750
That's why you don't have to do this like that with the dump pointers here.

09:20.960 --> 09:23.720
And we can.

09:25.780 --> 09:27.620
To test it like that here.

09:28.820 --> 09:30.500
Books are one.

09:31.860 --> 09:33.960
And data here.

09:38.330 --> 09:43.280
And as you can see here, this won't compile either.

09:43.280 --> 09:48.860
So this is not the correct, correct coding style here.

09:48.860 --> 09:56.030
And you will learn more about and how to correct this syntax in next lecture.
