tags:

views:

855

answers:

6

This is a question that's been nagging me for some time. I always thought that C++ should have been designed so that the "delete" operator (without brackets) works even with the "new[]" operator.

In my opinion, writing this:

int* p = new int;

should be equivalent to allocating an array of 1 element:

int* p = new int[1];

If this was true, the "delete" operator could always be deleting arrays, and we wouldn't need the "delete[]" operator.

Is there any reason why the "delete[]" operator was introduced in C++? The only reason I can think of is that allocating arrays has a small memory footprint (you have to store the array size somewhere), so that distinguishing "delete" vs "delete[]" was a small memory optimization.

+22  A: 

It's so that the destructors of the individual elements will be called. Yes, for arrays of PODs, there isn't much of a difference, but in C++, you can have arrays of objects with constructors and destructors.

Now, your question is, why not make new and delete behave like new[] and delete[] and get rid of new[] and delete[]? I would go back Stroustrup's "Design and Evolution" book where he said that if you don't use C++ features, you shouldn't have to pay for them (at run time at least). The way it stands now, a new or delete will behave as efficiently as malloc and free. If delete had the delete[] meaning, there would be some extra overhead at run time (as James Curran pointed out).

David Nehme
Actualy, when u use new int[1] it stores on the head of the array before it's first data, the size of it. So, using delete instead of delete[] will not free that part of the memory.
Rodrigo
+1  A: 

delete [] ensures that the destructor of each member is called (if applicable to the type) while delete just deletes the memory allocated for the array.

Here's a good read: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=287

And no, array sizes are not stored anywhere in C++. (Thanks everyone for pointing out that this statement is inaccurate.)

Ates Goral
Don't agree with your last statement. The compiler has to know the array size in order to call the destructor for each object in the array. I think you're confusing this with the fact that C++ doesn't do bounds-checking on arrays.
Mike Spross
Oh true. I thought you were suggesting that the size would be stored as part of the array data structure (buffer). Yes, the compiler would probably have to store the size info somewhere...
Ates Goral
One approach is to store the size and number of elements in the word before the start of the array. This is called a cookie.
Dynite
also, delete does call the destructor - for one element.
peterchen
+3  A: 

Marshall Cline has some info on this topic.

Fred Larson
+5  A: 

Since everyone else seems to have missed the point of your question, I'll just add that I had the same thought some year ago, and have never been able to get an answer.

The only thing I can think of is that there's a very tiny bit of extra overhead to treat a single object as an array (an unnecessary "for(int i=0; i<1; ++i)" )

James Curran
Plus a tiny bit of memory to store the size.
Martin York
Yep, I'd bet that the memory overhead was considered unacceptable. Possibly the loop was too.
Steve Jessop
+5  A: 

Damn, I missed the whole point of question but I will leave my original answer as a sidenote. Why we have delete[] is because long time ago we had delete[cnt], even today if you write delete[9] or delete[cnt], the compiler just ignores the thing between [] but compiles ok. At that time, C++ was first processed by a front-end and then fed to an ordinary C compiler. They could not do the trick of storing the count somewhere beneath the curtain, maybe they could not even think of it at that time. And for backward compatibility, the compilers most probably used the value given between the [] as the count of array, if there is no such value then they got the count from the prefix, so it worked both ways. Later on, we typed nothing between [] and everything worked. Today, I do not think "delete[]" is necessary but the implementations demand it that way.

My original answer (that misses the point) ::

"delete" deletes a single object. "delete[]" deletes an object array. For delete[] to work, the implementation keeps the number of elements in the array. I just double-checked this by debugging ASM code. In the implementation (VS2005) I tested, the count was stored as a prefix to the object array.

If you use "delete[]" on a single object, the count variable is garbage so the code crashes. If you use "delete" for an object array, because of some inconsistency, the code crashes. I tested these cases just now !

"delete just deletes the memory allocated for the array." statement in another answer is not right. If the object is a class, delete will call the DTOR. Just place a breakpoint int the DTOR code and delete the object, the breakpoint will hit.

What occurred to me is that, if the compiler & libraries assumed that all the objects allocated by "new" are object arrays, it would be OK to call "delete" for single objects or object arrays. Single objects just would be the special case of an object array having a count of 1. Maybe there is something I am missing, anyway...

Malkocoglu
+2  A: 

Adding this since no other answer currently addresses it:

Array delete[] cannot be used on a pointer-to-base class ever -- while the compiler stores the count of objects when you invoke new[], it doesn't store the types or sizes of the objects (as David pointed out, in C++ you rarely pay for a feature you're not using). However, scalar delete can safely delete through base class, so it's used both for normal object cleanup and polymorphic cleanup:

struct Base { virtual ~Base(); };
struct Derived : Base { };
int main(){
    Base* b = new Derived;
    delete b; // this is good

    Base* b = new Derived[2];
    delete[] b; // bad! undefined behavior
}

However, in the opposite case -- non-virtual destructor -- scalar delete should be as cheap as possible -- it should not check for number of objects, nor for the type of object being deleted. This makes delete on a built-in type or plain-old-data type very cheap, as the compiler need only invoke ::operator delete and nothing else:

int main(){
    int * p = new int;
    delete p; // cheap operation, no dynamic dispatch, no conditional branching
}

While not an exhaustive treatment of memory allocation, I hope this helps clarify the breadth of memory management options available in C++.

Aaron