Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Jun 16, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Mixing delete and delete[]

I am programming with Visual C++ 5.0 and the class library StarView from StarDivision. Looking at the Source from StarView, I found bad code:

void** p = new (void*)[100];
delete p; <-- wrong operator, should be delete[] instead
Could this be the reason for the following warnings during program execution in debug mode:

Could not free object from heap using pointer xyz.

Can the library handle this problem and free the objects correctly?

Probably not.

You are correct that arrays allocated using new should normally use the delete [] syntax. However, it is not always necessary to use this syntax.

The delete operator signifies that you are freeing the memory for one or more objects. But C++ does more than just release the memory. It also calls the destructors for objects you are deleting. If you have an array of class objects, then C++ will call the destructor for each object in the array. This is why the delete [] syntax is used. It tells C++ that it must destroy all the objects in the array.

However, many objects, such as char and void* have no destructors. For data types such as these, it really doesn't make any difference if you use delete or delete [].

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date