4

Possible Duplicate:
Why [] is used in delete ( delete [] ) to free dynamically allocated array?
Why does C++ still have a delete[] AND a delete operator?

I'm wondering what's their difference and I know the obvious answer some might say, that one is to delete an array and the other is to delete a single object but I'm wondering why should there be two different deletion methods for these two operations? I mean delete is basically implemented using C free method which doesn't care if the pointer is actually pointing toward an array or a single object. The only reason I can think of is two be able to know if it's an array and call destructor for each cell instead of only the first object but that wouldn't also be possible since compiler can not guess the length of array just looking at it's pointer. By the way though it's said to invoke undefined behavior to call delete for memory allocated with new[] I can't imagine anything that could possibly go wrong.

Community
  • 1
  • 1
Ali1S232
  • 3,373
  • 2
  • 27
  • 46
  • Note that they arent implemented at compiler level but in standard library as `::operator delete(void*)` and `::operator delete[](void*)` – Zaffy Jan 27 '13 at 23:00
  • 1
    @Zaffy: not quite. you're talking about the default deallocation functions. for one they're only the default ones, and for another, deallocation is only the last part of what a `delete` expression does. – Cheers and hth. - Alf Jan 27 '13 at 23:02
  • You might find it entertaining to read the Itianum C++ ABI, section "array cookies". – Kerrek SB Jan 27 '13 at 23:06

3 Answers3

3

As you have discovered the compiler needs to know the length of an array (at least for non-trivial types) to be able to call destructors for each element. For this new[] typically allocates some extra bytes to record the element count and returns a pointer to the end of this bookkeeping area.

When you use delete[] the compiler will look at the memory before the array to find the count and adjust the pointer, so that the originally allocated block is freed.

If you use delete to destroy a dynamically allocated array, destructors for elements (except the first) won't be called and typically this will end up attempting to free a pointer that doesn't point to the beginning of an allocated block, which may corrupt the heap.

JoergB
  • 4,383
  • 21
  • 19
1

but that wouldn't also be possible since compiler can not guess the length of array just looking at it's pointer

That's not really true. The compiler itself doesn't need to guess anything, but it does decide which function to call to free the memory based on the operator it sees. There is a separate function dedicated to releasing arrays, and this function does indeed know the length of the array to be freed so it can appropriately call destructors.

It knows the length of the array because typically new[] allocates memory that includes the array length (since this is known on allocation) and returns a pointer to just the "usable" memory allocated. When delete[] is called it knows how to access this memory based on the pointer to the usable part of the array that was given.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

When you allocate memory using new[], the compiler not only needs to construct each element, it also needs to keep track of how many elements have been allocated. This is needed for delete[] to work correctly.

Since new and delete operate on scalars, they don't need to do that, and could save on a little bit of overhead.

There is absolutely no requirement for new to be compatible with delete[] and vice versa. Mixing the two is undefined behaviour.

NPE
  • 486,780
  • 108
  • 951
  • 1,012