Archived

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

Diodor

Lazy question : delete or delete[] ?

Recommended Posts

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
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
New always allocates a single contiguous block of memory. Delete has to call all of the destructors for you, so it needs to know if it is an array or not (theoretically, it doesn''t matter often in reality, but it is better to be safe).

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
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
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
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