Microsoft KB Archive/93605

= Microsoft Knowledge Base =

Deleting Dynamically Allocated Arrays in C++
Last reviewed: July 31, 1997

Article ID: Q93605

7.00 | 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00

MS-DOS | WINDOWS            | WINDOWS NT kbprg

The information in this article applies to:

The Microsoft C/C++ Compiler (CL.EXE), provided with:

- Microsoft C/C++ for MS-DOS, versions 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51 and 1.52   - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0

SUMMARY
In a C++ program, use the following syntax to free a dynamically allocated array (an array allocated using the &quot;new&quot; operator):

delete [] pointer; If an application fails to use this syntax, the following occurs:


 * The destructor is called only for the first element of the array.
 * The application does not call the global &quot;delete&quot; operator. The global &quot;delete&quot; operator is called for an array of any type. This is documented in the Microsoft C/C++ &quot;Language Reference&quot; manual for version 7.0 and in the description for &quot;The operator delete Function&quot; in the Visual C++ 32-bit Edition, version 4.0, Books Online.

The Microsoft &quot;C++ Tutorial&quot; manual for version 7.0 provides a detailed description of deleting arrays when the brackets ([]) are specified and are omitted. Figure 6.2 provides a graphical representation of the problems that result from omitting the brackets while Figure 6.3 presents the correct behavior.

Although it is not necessary to use the brackets with an array of a built-in type (such as char or int) because no destructor is called, it is a good practice to include the brackets anyway. Doing so will prevent any problems if the element type is changed to a user-defined type in the future.

MORE INFORMATION
The following sample code demonstrates deleting an array of objects. First, two arrays of objects are dynamically allocated, the first of CMyClass1 objects and the second of CMyClass2 objects. For each object, its constructor is called. Then, the arrays are deleted. Since the delete statement for the array of CMyClass1 objects does not include the brackets, the application calls the destructor only for the first object in the array. The second delete statement shows the necessary syntax to delete all array objects. The application's output reveals which constructors and destructors are called.

Sample Code
/* Compile options needed: None

class CMyClass1 { public:
 * 1) include 

CMyClass1 {     cout << &quot;CMyClass1 Ctor called\tthis = &quot; << this << endl; }  ~CMyClass1 {     cout << &quot;CMyClass1 Dtor called\tthis = &quot; << this << endl; } };

class CMyClass2 { public:

CMyClass2 {     cout << &quot;CMyClass2 Ctor called\tthis = &quot; << this << endl; }  ~CMyClass2 {     cout << &quot;CMyClass2 Dtor called\tthis = &quot; << this << endl; } };

void main(void) {

CMyClass1 * p1CMyClassArray = new CMyClass1[16]; CMyClass2 * p2CMyClassArray = new CMyClass2[16];

delete p1CMyClassArray;    // only destructor of first elem is called delete [] p2CMyClassArray; // destructors for all objects called }