- [Instructor] The new and elite operators
are used to allocate and free memory in C++.
These operators are specific to C++
and are not available in C.
Here is new-delete.cpp from chapter four of the exercise
files.
We create a constant with the value of 1024.
And then down here, we allocate a pointer to a long integer.
And then we initialize that pointer with the new operator
and the new operator allocates space of type long int
and it allocates these many objects.
And so here we have 1024 we're allocating space for an array
of 1024 long integers.
And this is all wrapped up in this try-catch block for
catching an exception.
We'll talk more about exceptions later on in this course,
but this is an example of an exceptions use.
And then we initialize the array with this four loop.
And when we're all done with the array here on line 26,
we use the delete operator to deallocate the space and free
it up in memory.
If we don't do that,
we end up with memory leaks and vulnerabilities in our code.
So every time you allocate memory with new,
you must free that memory with delete.
So let's go ahead and build and run this,
and you see that it works as expected.
We allocated the space and we've initialized it and deleted
the space.
Now, suppose something bad happens.
Suppose we try to allocate space for more memory than we
have. So I'm going to add 12 zeros after this 1024,
and we're going to save and build and run again.
And you notice that well,
here's our allocate space for message,
which is this message here.
And then here's a bunch of jibberish from the runtime that
says, can't allocate the region, blah, blah, blah.
And here's our error message, cannot allocate memory,
bad-alloc.
And that's this message here because we caught the exception
and we exited gracefully with the exit code one there with
that return one.
So that's how this is typically done with an exception
block, with catching the exceptions with the try block.
Now, if you don't want to use exceptions, you don't have to,
exceptions is the recommended way to do this,
but we can go ahead and get rid of that.
And instead of this catch,
we can do if not IP. So if the alloc failed,
then the pointer will be zero.
In order to do that in order not use the exception
not have a thrown exception.
We need to add a parameter here in this standard namespace
called Nothrow.
And this will tell the new operator to not throw that
exception. And instead,
let us test the pointer to see whether or not the allocation
succeeded now,
because we're no longer in the exception block.
We don't have this what anymore. So instead of Fprint,
if I'm just going to use Fputs and for Fputs,
the stderr that needs to be in the second argument
and we no longer have this replacement there.
So this is what it looks like without the exceptions.
We have the Nothrow here and we're testing with an IF to
test whether or not the pointer was allocated.
And when I build and run this,
you notice we get a very similar result. It says,
cannot allocate memory.
It doesn't have that exception code over here for most
purposes. That's fine.
Exceptions is the recommended way to do this.
And if you don't want to use exceptions,
you can do it this way.
Now it's worth noting that this allocating a giant amount of
space.
Doesn't actually work on windows.
Windows does a compile time test and says, no,
that's too much memory,
but on compilers where you can cause the error this way,
this is how you would cause that error.
And we're really just doing that to cause the error under
normal circumstances
You're not going to try to allocate more memory than you
have. Are you, and so we'll go ahead and build and run this.
And we see that it executes as expected.
So the new operators used for allocating space for objects
and C++ every object allocated with new must be
destroyed with delete.
Otherwise your program will leak memory.