Accessing out of bounds.
When you allocate a buffer, whether on the stack or on the free store, you get a pointer.
There is 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 index 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 for loop which this for loop will try to reach the out of bounds.
So this for loop will try to reach the fourth element of this our array element here.
So for make an integer zero here and while integer is less than four.
Uh, plus.
Plus.
Let's make it five, actually.
So here right here.
Or actually make it four because, um, I want to tell something and we'll connect this subject to,
uh, another here plus equals array.
A plus one.
Here.
This is going to be what happens when E equals three here.
So let's print it out.
Actually.
See out.
Okay.
And make it variable.
Integer.
A e.
Here.
Integer A and end line.
Here.
So when you use indexing, you have to keep reminding yourself that arrays are indexed from zero.
Arrays are in index.
From zero.
So last array equals array minus one.
So.
As you can see here, if we increase this to, for example, this 4 to 10, as you can see, we will
get the random numbers here.
The.
And run.
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, uh, like dangerous.
So this applies to memory allocated on the stack and to memory.
Dynamically allocated.
So now I want to create some function will that will return character here get and.
Here.
This will return the character.
See here.
And.
Hello.
And return.
See here.
So this code 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 six character.
I mean, the last character is null here.
So.
And this null 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 a 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 to 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 parameters 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 color.
So in with with this method, it's more safer than this here.
So this leads on to another issue.
So if you call delete 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 alleviate this problem, you can get into the habit of assigning a pointer to null pointer here.
So when you delete it and check for the null before using assignment.
So it's more a 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 lecture.
So here.