Jump to content
  • Advertisement
Sign in to follow this  
i_luv_cplusplus

delete vs delete[]

This topic is 3554 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

Hello, is there some difference between delete and delete[]? I know the latter should be used for arrays. I am asking, because it is working for me, but I am afraid I am venturing into the realm of undefined behaviour, and it's gonna break as soon as I don't expect it. What happens if I use delete[] on a non-array pointer/variable? Is it safe to do?

Share this post


Link to post
Share on other sites
Advertisement
Removed due to wrongness :)

Edit: but I was reading that with new[] compilers may add arbitary data with the array, delete[] may look for this data and it may not be present if new was called for the object created. Probably the only good advice is to match new to delete and new[] to delete[].

[Edited by - Guthur on January 22, 2009 6:00:21 PM]

Share this post


Link to post
Share on other sites
The only thing that is realy save to do here is using delete[] on anything you allocated using new[]. Using delete on an array or delete[] on a non array pointer might work in some cases/on some compilers but you could never be shure.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guthur
I think you might be ok calling Delete[] on non array's but calling delete on an array is a memory leak, you only delete the pointer to the first element, and the rest of the array will be lost.

I hope thats the case anyway :p


Calling the wrong version of delete is undefined behavior. Don't do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Calling the wrong version of delete is undefined behavior. Don't do it.


Agreed... delete [] operates on arrays and when delete [] is called on an array of size N elements, then for class objects, N calls to the classes' destructor must be called in order for delete [] to release ALL memory properly.

And how exactly is that magic number N stored? Well that is compiler specific, but some compilers store N JUST before the first element in the array.

So... Using delete [] on a single object is VERY dangerous... delete [] may interpret memory just before the array as the number of objects to destroy before memory is deallocated and this number could be anything.

And of course, calling delete on an array of objects leads to the obvious memory leaks outlined earlier.

Stick to delete for SINGLE objects and delete [] for an ARRAY of objects.

For in-built types, I think SOME compliers might not bother store the array size N, since in-built types don't have destructors, but, even so, calling delete [] on single objects, whether in-built or user defined, is STILL bad practise.

Share this post


Link to post
Share on other sites
Quote:
Original post by i_luv_cplusplus
is there some difference between delete and delete[]? I know the latter should be used for arrays.


The latter must be used for array-allocations that were allocated with new[]. The former must be used for scalar (single value) allocations that were allocated with new. This is "some difference", don't you think? :) What they actually do, behind the scenes, is not (for now) your concern. The point of using C++ instead of, say, assembly, is so that you don't have to worry about those details.

Of course, you could, in nearly all cases, avoid the difficulties associated with matching up new to delete and new[] to delete[] (not to mention making sure things get deleted exactly once) by just using standard library containers. :)

Quote:
I am asking, because it is working for me, but I am afraid I am venturing into the realm of undefined behaviour, and it's gonna break as soon as I don't expect it.


A good instinct. That's the problem with undefined behaviour :/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!