Memory and the C++ standard library here.
This C++ Standard library provides various classes to allow you manipulate collection of objects.
These classes called the s t L Standard Template Library.
So this provides a standard way to insert items into collection objects and ways to access the items
and iterate wrote entire collections.
This is called the Iterator.
Here, the SDL defines collections classes that are implemented as Quest stacks or as a vectors with
a random axis 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++ built in arrays.
The first let's get began 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 size 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 builtin standard.
Library.
All right.
So we can also use the stack based array class in C++ 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 all located 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 this is passed as a template parameter like that here, for example.
Let's include a include array here.
Array and array.
Here, Integer four.
And here, 1 to 3.
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 initialize the array with an initialize list, but not 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 range loop here.
So let's create a range load here integer E and here we can reset the array here, make it here.
Right here, and we're going to create the array in the jar.
There.
Firstly, we're going to create the array like that.
Yeah.
And we're going to pass iterate fraudulence error here at.
And each time we're going to print this on the screen.
We have to import and use the namespace here.
And rent it out.
As you can see, we printed our variables here.
The reason is that all implements the begin and end function that are required for this syntax.
So you can also use this like that here.
For example, for integer E zero opps integer E equals zero, while E is less than R that size here
and plus class E here and C out here are right here are error E and and 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 or that ten.
So this may cause the unexpected behavior at runtime.
Let's try it out.
As you can see here, there's a just reprint.
There's a bunch of zeros.
Here.
So or this may cause unexpected behavior or even some kind of memory faults.
So to guard against this, the class provides a function at which will perform perform a range take.
And if the index is out of range, the class will throw the C++ exception which here.
Here.
C++ 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 pen in its.
An integer pointer here.
So at runtime the function does not know the size of the buffer pass 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++ 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, Ops.
Here.
So let's create another array for this example and make comment out.
Common theme here.
Okay.
Integer array.
Yes.
And one, two, three, four.
This contains the 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 check or nor any non runtime check to catch this error.
But there is the already class will not allow such an error to happen because there's no automatic conversion
into a the pointer here.
So if you're interested in obtaining a dumb 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 a letter turned from some comment filed.
That's why you don't have to do this like that with a thumb pointer here.
And we can.
Testify like that here.
I was 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.