Previous Section  < Free Open Study >  Next Section

Leak #4

Arrays of pointers to objects are not arrays of objects. Another point of confusion related to the use of delete's square brackets is the fact that destroying arrays of object pointers is very different from destroying arrays of objects. As was shown in the preceding code, the square brackets on the delete function control the proper calls to each object's destructor when used for destroying arrays of objects. Some developers attempt to use the size argument as a method for controlling proper destructor calls for objects stored in arrays of pointers to objects. Adding a size argument to the delete call on an array of pointers to objects simply tells the compiler the number of pointers to destroy. Since pointers are considered primitive types in C++, this argument has no effect on destructor calls. The pointers are placed back on the heap, but all of the objects are kept in their allocated state. The result is memory leakage.

    Previous Section  < Free Open Study >  Next Section