Possible Duplicates:
How does delete[] “know” the size of the operand array?
( POD )freeing memory : is delete[] equal to delete ?
As I understand, the following
class A {};
A* a = new A;
//
delete A;
will result first in a call to operator new()
(the global one, or a specialized one provided by A) to allocate the right amount of memory and then to a call to the constructor of A. And when delete
is called, is will first call the destructor of A, and then call operator delete()
to deallocate the "right amount of memory".
As I read in TC++PL, this "right amount of memory" is determined like that:
To deallocate space allocated by new, delete and delete[] must be able to determine the size of the object allocated. This implies that an object allocated using the standard implementation of new will occupy slightly more space than a static object. Typically, one word is used to hold the object's size.
This makes sense. But where is this word stored to be accessible by delete
? Just before the address pointed by the new pointer ? So that delete
can obtain the size to be deleted by accessing a-sizeof<void*>
?
Can you clarify this ?
I think the answer to that may help me to understand how delete []
works. I understand how new []
will work, and that delete []
will first call the destructors for "all the objects of the array" and the deallocate all this memory...
But how can delete []
know the size of the array ?
Thanks for your helpful answers !