Advertisement

Lazy question : delete or delete[] ?

Started by November 09, 2001 01:38 AM
6 comments, last by Diodor 23 years, 1 month ago
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
In that case it doesn''t matter, but you should use the [] if you created an array with new (with a [] ) and not a single item.

[Resist Windows XP''s Invasive Production Activation Technology!]
Advertisement
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?
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!]
Hi,

If you were to use "new char", then just use "delete".

But since you used "new char [64]" you should use "delete []".
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.
Advertisement
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[].
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

This topic is closed to new replies.

Advertisement