previous | start | next

## Deallocating Dynamic Memory

- Reclaiming a pointer variable does not automatically reclaim the object to which it points.
- (Common Error 10.4) A memory block that is not deallocated is called a memory leak.
- Leaked memory can cause the heap to run out of memory.
  - Program crashes.
  - Computer freezes up.
- When a pointer is defined it points to a random address; using this random address causes and error.

  >     Employee* boss;
  >     string name = boss->get_name(); // NO!! boss contains a random address

- After you `delete` the value attached to a pointer, you can no longer use that address.

  >     delete boss;
  >     string name = boss->get_name(); // NO!! boss points to a deleted element

- (Common Error 10.3) Such pointers are called dangling because they do not point to a valid object.

previous | start | next