Accessing out of bonds.
When you allocate a buffer, whether on the stack or on the free store, you get a pointer.
There is a little to stop you from accessing memory you have not allocated either before or after the
position of the buffer.
This means that when you use pointer arithmetic or indexed access on arrays that you check carefully
that you are not giving to access the data out of bounds.
Sometimes the error may not be immediately obvious, but now I want to create some code that will show
how this out of bounds works here.
One, two, three, four.
Here.
Okay.
Integer array here and.
Let's make a four look rich this fall.
We'll try to reach the out of bounds.
So this fall we'll try to reach the fourth element of this, our array element here.
So for making integer zero here, and while integer is less than four.
Uh, e plus.
Plus.
Let's make it five, actually.
So here.
All right.
Here.
Or actually make it four because I want to tell something and I will connect this subject to another
here.
Class equals array.
E plus one.
Dear.
This is going to be what happens when, uh, e equals three here.
So let's print it out.
Actually see out.
Okay.
And make it readable.
Integer a.
Here.
Integer A and and one.
Here.
So when you use indexing, you have to keep reminding yourself that arrays are indexed from zero.
Arrays are in index.
Rome zero.
So last array equals array minus one.
So.
As you can see here, if we increase this tool, for example, this 4 to 10.
As you can see, we will get the random numbers here.
Mr. Pendry, Iran.
Here.
So now I want to talk about something that pointers have in the allocated memory.
So that's the main reasons of why pointers are like dangerous.
So this applies to memory.
I'll in the stack and to memory dynamically allocated.
So.
Now I want to create a sum function will that will return character here.
And.
Here.
This will return the character see here and allow.
Our law and return see here.
So this court allocates a buffer of six characters and then initialize it with the five characters of
the string.
Literal.
Hello.
Here.
So and this by the sixth character, I mean, the last character is null here.
So and this no means the termination character in the strings and characters here.
So the problem is that once the function finishes, the stack frame is turned down so that the memory
can be reused.
And the pointer will point to memory that could be used by something else.
So this error is caused by poor programming here, but it may not be as always as in this example.
So if the function uses several pointers and performs the pointer assignment, you may not immediately
notice that you have returned a pointer to a stack allocated object.
So the best course of action is simply not return raw pointers from functions.
But if you do want to use this style of programming, make sure that the memory buffer is passed in
through the parameter so the function does not own the buffer.
So when the memory buffer is passed through the parameter or is dynamically allocated and you are passing
ownership to the caller.
So in with this method, it's more safer than this here.
So this leads on the another user.
So if you call the later on a pointer and then later in your code, try to access the pointer, you
will be accessing memory that is potentially being used by other variables.
So to evaluate this problem, you can get into the habit of assigning a pointer to new TR here.
So when you delete it and check for the note TR before using assignment, so it's more good practice
here.
So alternatively, you can use smart pointer object, which will do this for you here, but smart pointers
will be covered in next lectures here.