Release of Memory
delete operator deallocates the memory allocated by the
Point* p = new Point(1.0, 2.0);
If the deleted object belongs to a type hierarchy, the destructor of the object and the destructors of all base classes will be automatically called. If the destructor of the base class is virtual, we can destroy the object with a pointer or reference to the base class.
After the memory of the object is deallocated, further access to the object results in undefined behavior. We have to point the deleted object’s pointer of the object to a different object.
deleteto deallocate an object allocated with
newwill result in undefined behavior.
We have to use the operator
delete for the deallocation of a C array that was allocated with
Point* p = new Point;
In contrast to
delete calls all the destructors of the C array.
deleteto deallocate an object allocated object with
newwill result in an undefined behavior.
Let’s see a few examples of memory management in the next lesson.