Foo* set = new Foo[100];
// ...
delete [] set;
You don\'t pass the array\'s boundaries to delete[]
. But where is that information stor
It's defined in the C++ standard to be compiler specific. Which means compiler magic. It can break with non-trivial alignment restrictions on at least one major platform.
You can think about possible implementations by realizing that delete[]
is only defined for pointers returned by new[]
, which may not be the same pointer as returned by operator new[]
. One implementation in the wild is to store the array count in the first int returned by operator new[]
, and have new[]
return a pointer offset past that. (This is why non-trivial alignments can break new[]
.)
Keep in mind that operator new[]/operator delete[]
!=new[]/delete[]
.
Plus, this is orthogonal to how C knows the size of memory allocated by malloc
.