Archived

This topic is now archived and is closed to further replies.

Diodor

Lazy question : delete or delete[] ?

Recommended Posts

Diodor    517
What's the difference between delete and delete[] ? Will there be any harm if I use one instead of the other like this:
    
char *str = new char[64];
delete str;
    
- edit: i wrote delete c; instead of delete str - too lazy to reread my post Edited by - Diodor on November 9, 2001 3:49:29 AM

Share this post


Link to post
Share on other sites
Diodor    517
Well, new char[64]; does have a nice pair of [s in there

So when is new[] allocating a single memory block (new char[64] does) and when is it allocating separate memory for separate objects? Does it have to do with C++ classes?

Share this post


Link to post
Share on other sites
Sinner_Zero    122
oK, why so much thinking in this.

Use "delete" and you only clear the first value(I bet thats not the right word) in the array, use delete[] and you release all of the values.

Thus by using only "delete" with an array your slowly wasting all of the systems memory.

Share this post


Link to post
Share on other sites
IaM    122
delete[] calls the destructor of all the objects allocated with new[], while delete only call the destructor of the first object.
But if the object that you''re constructing doesn''t do anything in the destructor, then delete[] and delete are the same, but is more correct to use delete[].

Share this post


Link to post
Share on other sites
Kneelz    122
I always use delete{}. It does a delete[] on delete. IMHO it calls all available destructors of the current system and then it is shut down (This is AFAIK a bug in Windows).

Kneelz

Share this post


Link to post
Share on other sites