It’s
convenient and optimally efficient to create automatic objects on the stack,
but to solve the general programming problem you must be able to create and
destroy objects at any time during a program’s execution, particularly to
respond to information from outside the program. Although C’s dynamic
memory allocation will get storage from the heap, it doesn’t provide the
ease of use and guaranteed construction necessary in C++. By bringing dynamic
object creation into the core of the language with
new
and
delete,
you can create objects on the heap as easily as making them on the stack. In
addition, you get a great deal of flexibility. You can change the behavior of
new
and
delete
if they don’t suit your needs, particularly if they aren’t
efficient enough. Also, you can modify what happens when the heap runs out of
storage. (However,
exception
handling
,
described in Chapter 16, also comes into play here.)