I want to show you some of the pitfalls that you should avoid when using dynamic memory.
So here's a simple one.
Say add and pointer and it points to this new integer and we say each winter this this is OK.
Then you say lead again.
So double delete a pointer to memory.
Remember that once you delete the memory the memory is gone back to the heap.
So your program doesn't really control it anymore and it's part of the heap now.
So once you delete it again really only bad things will happen.
So instead what you should do is always say this to set it to null pointer and so this is good.
So Never leave something in another example something to not do is say we had pointer of 10 integers
and it doesn't have to be.
Could could still we could do it this way too but it's going to do it for a difference.
And let's say we had a different integer an actual integer set to five year.
So this point your points to the head of these 10 contiguous integers in memory.
Right.
So this array of 10 integers sets went to the head.
But what if you did this.
So point it to something else.
OK.
So this is very bad.
OK.
This is what's called a memory leak because there's no way to reclaim where it regionally pointed to.
Right.
Like we pointed originally to the head of some memory which gave some address back right.
But now we're pointing somewhere else and our owning pointer.
Originally this was the owning pointer.
Right now kind of points somewhere else.
Which is very bad so never ever ever do this.
OK.
So in a similar way
here can say something like this.
Just put the address
winter
this.
And then remember we could do this you could increment the pointer right.
And that would give you the address of the next one.
The next item in the array right.
So if we did that and ran it of course you'd get.
You know it's four bytes more.
Right.
So the next one which is fine but what you've really done here is now you don't point to the head anymore.
OK point to the initial address.
Right.
So you're kind of pointing to the next one instead.
OK so you should never you should always leave the initial pointer owning pointer alone and access it
through some other means like making a new pointer and pointing it to the memory.
So any owning pointer should always point exactly to where pointed to when you knew the memory.
OK.
And it would be even worse if you did something like this.
This would have.
Now you said delete point here and you kind of forgot that you incremented it.
Well see what happens.
Oh not good.
Right.
So it says pointer being freed it's say was not allocated.
It set a breakpoint and all this all this stuff right.
So it's really saying hey you're deleting something.
In fact we even do it wrong.
Right.
It should be like this.
Even OK you're deleting something but it wasn't.
It's not to the same address as it should be.
Right.
It's not pointing to the original addresses really what this is saying.
OK.
So this is very very bad to do so always just keep the same or if it's an owning pointer just keep it
pointing to where it originally pointed to.
OK.
Access it through some other pointer.
So if you really wanted to do this we should say.
Pointer to right.
Hey goes to pointer.
And then we use pointer to where here.
Are two parts plus K.
And then we could delete the memory OK of the owning pointer of your and this should be OK.
Jay so always access it through a different pointer and keep the owning pointer the same
so other pitfalls again deleting pointers you don't own right.
So never do that.
Not assigning the null pointer after the memory was ready.
Here's a nother example let's say you had
her course new integer 10
integers and never do this.
OK you're here sort of.
This is the same as with the arrays you're kind of are assigning.
Say so you're signing outside
your index range right.
Ok it's very bad to do this.
We don't know what will happen.
If you do this it might crash it might not crash.
Hopefully it will crash because at least then you'll see where is crashing.
Right.
So that that would this is bad to do.
And of course if you do something like this say it and then you delete that with the array image mismatch
right.
Her mismatch.
So you knew that with only the regular new but you're deleting it with the array delete.
So don't do this.
This is also very bad.
And of course vice versa.
Right.
So these are some of the pitfalls if you follow the guidelines outlined by this time you should.
You should usually be OK.
I tend not to use dynamic memory so much but it's useful in some search situations and in the next section
when we do our hangman game you'll see just how useful it can be.