* Colander:
Hi There,
Jean-Marie Epitalon said:
Hello,
please someone could explain to me why brackets are needed when deleting a
dynamic array, like :
int * my_array = new int[5];
...
delete [] my_array
http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.11
Yes, but I think Marshall could have explained why.
The original motivation was the principle of not paying for what you
don't get. C++ needs to know the number of array elements in order to
call their destructors. And it's natural to implement C++ new in terms
of C malloc, but malloc may not be storing the element count itself: it
may in turn be using an OS allocator (say) that does that transparently,
with the count not available. So instead of requiring malloc to add an
extra array length field (i.e. allocate four or more bytes more than
actually requested), new[] does that and plain new doesn't. Which means
that delete[] must adjust (compensate) for the shenanigans of new[].