O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Deleting from the Heap

Deleting a heap-allocated pointer releases the storage it points to. delete comes in two forms: with [] (to delete an array), and without (to delete a regular variable).

Deleting is dangerous. Imagine what happens when you run this:

char *SomePointer = NULL;
delete SomePointer;

or this:

char *SomePointer;
delete SomePointer;

or:

char *SomePointer = new char;
delete SomePointer;
delete SomePointer;

or:

char *SomePointer = new char;
delete SomePointer;
char Stuff = *SomePointer;

As with using an invalid pointer or accessing beyond the end of an array, the consequences are unpredictable.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required