Archived

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

Lazy question : delete or delete[] ?

This topic is 5910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 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 on other sites
Hi,

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

But since you used "new char [64]" you should use "delete []".

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