previous | start | next

## Deallocating Dynamic Memory

- When you make a variable of a certain type, the object is allocated on the run-time stack. This object automatically goes away when the program leaves the block in which the variable is allocated:

  >     void f()
  >     {
  >        Employee harry; // memory for employee allocated on the stack
  >        . . .

  >     } // memory for employee object automatically reclaimed

- Value that are allocated on the heap do not follow this automatic allocation mechanism. You must reclaim the memory using the `delete` operator.

  >     void g()
  >     {
  >        Employee* boss;
  >        boss = new Employee(. . .);
  >           // memory for employee object allocated on the heap
  >        . . .
  >        delete boss; // memory for employee object manually reclaimed
  >     } // memory for boss automatically reclaimed

previous | start | next