delete vs delete[]

Started by
5 comments, last by Zahlman 15 years, 2 months ago
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?
OpenGL fanboy.
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]
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
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.
C++ FAQ Lite 16.13
Hope that helps.
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.
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.
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 :/

This topic is closed to new replies.

Advertisement