To destroy a dynamically allocated object and free the space for the object, use the delete
operator as follows:
delete timePtr;
This statement first calls the destructor for the object to which timePtr points, then deallocates the memory associated with the object, returning the memory to the free store.
Common Programming Error 10.2
Not releasing dynamically allocated memory when it’s no longer needed can cause the system to run out of memory prematurely. This is sometimes called a “memory leak.”
Error-Prevention Tip 10.1
Do not delete memory that was not allocated by new. Doing so results in undefined behavior.
Error-Prevention Tip 10.2
After you delete a block of dynamically allocated memory be sure not to delete the same block again. One way to guard against this is to immediately set the pointer to nullptr. Deleting a nullptr has no effect.