The memory lifetime memory allocated by new keywords will remain valid until you call the delete here.
So this means that you may have memory with long lifetimes and the code may be passed around various
functions in your code.
So here, consider this.
Here, let's make p one new integer 4 to 2.
Here an integer key to do something.
This is the function that we will create.
Yes.
Delete P one and p one equals no p t r and here make the do something function here.
Integer here.
Do something.
And in return integer.
Here.
Or make.
Let's create a variable here integer my well and my.
This return to my Val here.
Here.
As you can see here, we cannot be we have to do typecast here.
And, uh, we got to do something here.
And as you can see here, we initialize P one to no TR But what about the P two here?
And these quote creates a pointer and initialize the memory it points to and then passes the pointer
to function, which itself returns the pointer here.
So since the p one.
Signs the P one pointer is no longer needed.
It is deleted and assigned to New Peter so that it cannot be used again.
So this code looks fine.
But the problem is what do you do with the pointer returned by the function?
Imagine here, let's change our function.
Now imagine this following function here.
P here.
Uh, equals ten.
Because it's not p.
P integer.
And return.
P integer.
So in in this code, the calling do something.
We actually we can if we do like this, we're going to get an error and that's why we have to call the
cast and yeah, yeah, it's okay to use this here before the function.
So this calling do something creates a copy of a pointer, but not copy of what it points to.
So this means that when P one pointer is deleted here.
The memory it points to is no longer available.
And so the pointer to here points to the invalid memory.
So this problem can be addressed using a mechanism called resource occlusion initialization.
This means error, i.e. here, which means using the features of C++ objects to manage resources.
So here I'm going to make it here.
Error a e here in C++, it needs classes and in particular copy constructors and distractors.
So a smart pointer class can be used to manage a pointer so that when it copied the memory it points
to is also copied here.
And the destructor is function that is called automatically when the object goes out of scope.
And so the smart pointer can use this free memory.
So don't worry, smart pointers and distractors will be covered in next lectures.