Question:
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?
Answer:
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 [].