Possible Duplicate:
How does delete[] “know” the size of the operand array?
Assume i have an array of objects created dynamically
Car *newcars = new Car[10];
delete [] newcars;
How does the compiler know that there are 10 objects that need to be deleted.
Because
new[]allocates more space than is needed for the objects. It also allocates space for the number of elements, and on debug systems maybe also the file and line number where the allocation took place, to help debug memory leaks.Including extra space in every allocation for the memory manager’s internal use is actually very common. When this happens and you have a buffer overflow, you may overwrite this extra space and whatever data the allocator kept there, resulting in “heap corruption”.