The new and delete operators are used to allocate memory for objects, and to destroy those objects safely.
- [Bill] The new and delete operators are used to allocate and deallocate memory in C++. Here, I have a working copy of new-delete.cpp from chapter one of the exercise files. Until now, we've been using dynamically allocated objects, but this is not always what you need. Sometimes you need to create an object and use it beyond the lifetime of a function or a block and destroy it later. This is what new and delete are for. So, here we have a simple class with three values and a combination constructor and a destructor, and if we look down here at our constructor and destructor, they put a message on the console, so that we know when they're being called because we'll see that this is important later, and then down here in main, we have a try catch block for catching an exception, and we create an object using new, we print out the values and then, we delete it.
And so, when I build and run, you see that the constructor gets called, the values are printed out, and then, the destructor is called. So, the constructor's called when memory is allocated for the object. This calls the constructor, creates the object, and assigns a pointer to that object and we use the pointer to dereference the getter functions, a, b, and c, and then, when we delete the object, that memory is freed and the destructor is called for the object, and so that's very important.
If you want to allocate an array of objects, you simply tell it how many in square brackets like this, but you must also use the corresponding array version of the delete operator. So, when I build and run, you see that our constructor gets called five times and our destructor gets called five times, one for each of the objects in the array. So, let's just put this back to the single version, and I want to show you how you can do this without using exceptions.
Exceptions is the preferred way to catch these errors, but there are some circumstances where you may not be able to throw an exception. So, if I take these here and I take them outside of that block and get rid of the block, now for the new operator, I give it the nothrow parameter inside of parentheses, and this tells it not to throw an exception, but if I'm not going to throw an exception, I still need to test and see if it was successful or not, and for this, new will return a null pointer if it's not successful.
And so, I can say if o1 is equal to null pointer, and then, give an error message and return a failure code. So, I can say puts("new c1 failed"), and return one, which is an error return code for main. And, you know, assuming it was successful, we print the value and delete, and you see that this works exactly the same way.
So, if you're not catching exceptions, remember to use no throw. The new operator is used for allocating space for objects in C++. Every object allocated with new, must be deallocated with the corresponding form of delete, otherwise, your program will leak memory.
- Classes and objects
- Constructors and conversion operators
- Class inheritance
- Smart pointers
- Move semantics
- Lambda syntax
- The C preprocessor
- Unit tests
- Building a custom string library