dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Mixing delete and delete[]

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 [].

DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

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