C++20 sections · 1024 units
Open in Course

The delete Operator

Manual cleanup

The delete operator frees heap memory and calls the destructor if applicable. Write delete p; where p is a pointer from new. After delete, the memory returns to the heap for reuse. Only delete pointers from new.

Deleting stack addresses or already-deleted pointers causes undefined behavior, often crashes. The pointer value isn't changed by delete; it becomes dangling. After deleting, set the pointer to nullptr: delete p; p = nullptr; This prevents accidental use of the dangling pointer and makes double-delete attempts safer.