Yes, delete[]
knows how many elements there are if you pass it a pointer that was returned from a call of new[]
. In your code, you have essentially:
int * p = new int[3];
// ...
delete [] p;
That's exactly what it is designed to do.
How does it do that? That's not really your business. As for the present example, in the quasi-standard Itanium ABI for C++, there is nothing to do, and you essentially end up with a call of free()
on the underlying memory. (So just use your existing understanding of free()
to fill in the blanks.) If the type of the array element was not trivially destructible, the compiler would need to store the number of array elements somewhere in some "hidden" place (which the Itanium ABI specifies) so that it can call all the destructors.