Memory Management: Memory Deallocation
In this lesson, we will learn about the second subsection of memory management - memory deallocation.
We'll cover the following
Memory Deallocation
delete
A new
with previously allocated memory will be deallocated with delete
.
Circle* c= new Circle;
...
delete c;
The destructors 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, the access to the object is undefined. We must initialize the pointer of the object to a point it to a different object.
The deallocation of
new[]
allocated object withdelete
has undefined behavior.
Get hands-on with 1400+ tech skills courses.