Jump to content
  • Advertisement

Archived

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

Diodor

Lazy question : delete or delete[] ?

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

If you intended to correct an error in the post then please contact us.

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
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?

Share this post


Link to post
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 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!