tags:

views:

59

answers:

3
+1  A: 

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".

Ben Voigt
How its implemented is up to the implementation - it could e.g. be using a map `(address -> infoStructure)` instead.
Georg Fritzsche
I didn't say that the extra space had to be adjacent to the array content... but it would be very unusual for it not to be.
Ben Voigt
A: 

Because the object has a destructor (even a default one) and it knows that there are 10 objects to destroy (they are deallocated as opposed to allocated with new). With the new keyword, it is typically allocated on the heap. The size is stored in the "head" segment.

0A0D
The question is "how does it know there are 10 objects to destroy?"
Ben Voigt
@Ben: The size is stored in the heap.. see my answer
0A0D
A: 

The memory manager keeps records of what is allocated to each address. So in fact the compiler does not know at compile time (after all such array allocations can be dynamic) but the run time libraries know when the memory is allocated.

Elemental