Memory and the C plus plus standard library here.
This C plus plus standard library provides various classes to allow you manipulate collection of objects.
These classes called the STL Standard Template Library.
So this provides a standard way to insert items into collection objects and ways to access the items
and iterate through the entire collections.
This is called the Iterators.
Here, the STL defines collections classes that are implemented as stacks or as a vectors with a random
access.
Here, these classes will be covered in depth in the next lecture.
So in this section we will limit the discussion to just two classes that behave like C plus plus built
in arrays.
The first, let's begin with the standard library arrays here.
The standard library arrays provides two containers that given random access via an indexer to the data.
So these two containers also allows you to access the underlying memory, since they guarantee to store
the items sequentially and contiguous in memory.
And they can be used when you are required to provide a pointer to a buffer.
So these two types are both templates, which means that you can use them to hold built in in the custom
types.
So these two collection classes are array and vector here.
Yeah.
So these are.
These are the Wilton standard.
Library.
All right.
So we can also use the stack based array class in C plus plus.
Here the array class is defined in the array here, like that array header file here.
So the class allows you to create fixed size arrays on the stack and with the built in arrays, they
cannot shrink or expand at runtime.
So since they are allocated on the stack, they do not require a call to a memory allocator at runtime.
But clearly they should be smaller than the stack frame size.
So this means that an array is a good choice for small array of items, so the size of an array must
be known at compile time and it is passed as a template parameter like that.
Here, for example, let's include a include array here.
Ouray and Ouray.
Here.
Integer four and here.
One, two, three, four.
So in this code, the first template parameter in the angled braces is a type of each item in the array.
And the second parameter is the number of items.
So here number of items and the type of the each item in the array here.
So this code initializes the array with an initialized list, but note that you still have to provide
the size of an array in the template here and this object will work like a built in array or indeed
any of the standard library containers with the ranged loop here.
So let's create a range loop here.
Integer E and here we created the array here.
Make it here.
Right here and we're going to create an array integer.
Here.
Firstly, we're going to create the array like that.
Here and we're going to pass iterate through this array here.
And each time we're going to print this on the screen.
We have to import and use the namespace here.
And print it out.
As you can see, we printed our variables here.
The reason is that our array implements the begin and end function that are required for this syntax.
So you can also use this like that here, for example.
INT for integer E zero oops, integer e equals zero, while e is less than r dot size here and plus
plus E here and C out here.
Our array here, our error E and n line here.
We can write it also like that.
It's the same output here.
The size function here.
As you can see here, we return the size of the array and the square braces.
Indexer gives the random access to the members of the array so you can access the memory outside of
the bounds of the array.
So for the previously defined array that has four members you can access like this array r ten.
So this may cause the unexpected behavior at runtime.
Let's try it out.
As you can see here, there's just a reprinted.
There's a bunch of zeros.
Here.
So or this may cause unexpected behavior or even some kind of memory fault.
So to guard against this, the class provides a function F which will perform, uh, perform a range
check.
And if the index is out of range, the class will throw the cplusplus exception, which, uh, here.
Here.
C plus plus exception named.
Out of range here.
So the main advantage of using an array.
Array object is that you get compile time checks to see if you are inadvertently passing the object
to a function as a dump pointer here.
Let's make another example here.
Void.
Here.
Void use an init.
An integer pointer here.
So at runtime the function does not know the size of the buffer passed to it.
And in this case, the documentation says that you must pass a buffer with ten integer types variables.
But as we see in C plus plus allows you allows a built in array to be used as a pointer.
So we can do it like that.
In practice use.
Ten.
Ah one.
So.
Oops.
Here.
Actually, let's create another array for this example and make comment out.
Comment it in here.
Okay.
Integer array.
Here.
And one, two, three, four.
This contains same variables as previous arrays.
Here.
And then this will read the past, the end of the buffer here.
As you can see here, we got an exception.
So this is no compiler checker nor any runtime check to um, catch this error.
But there is the array class will not allow such an error to happen because there is no automatic conversion
into a dump pointer here.
So if you insist in obtaining a dump pointer, you can do this and be guaranteed to have access to data
as a contiguous block of memory where the items stored sequentially.
So you can do it like that.
Here.
Let's compile it.
And, uh, here.
We got Elder turned one Subcommand filed.
That's why you don't have to do this like that with the dump pointers here.
And we can.
To test it like that here.
Books are one.
And data here.
And as you can see here, this won't compile either.
So this is not the correct, correct coding style here.
And you will learn more about and how to correct this syntax in next lecture.