Jeff said:
peter said:
Jeff said:
peter wrote:
Hello,
for a C++ delete:
class Test{};
Test * A = new Test[50];
delete [] A;
We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?
The same if I have an array
char *b = new char[40];
delete [] b;
how C++ compiler can know the right number of 40 to deallocate?
Thanks,
Peter
It usually allocates a little extra space before the array, in which
it puts housekeeping data like the size of the array.
It might break all other code.
What, the compiler might? I'm not sure what you mean... It really does
work this way (for many implementations).
I do not understand how compiler allocates extra space before the array
and then access it at the run time. Maybe that is the way it works.
Way back when, you actually did have to cite the number of elements
as part of the delete statement.
If I don't cite the number of elements, it works fine to call
the deconstructor 50 times for delete [] A.
That doesn't free the memory, it just deconstructs the objects.
I took it for granted that we don't need to specify the number of
elements for delete. Maybe I am wrong.
I just checked the previous thread discussing about delete [].
here is the quote from "Unforgiven <
[email protected]>":
If you say:
One *B = new One;
Memory is allocated and the constructor is called.
If you say:
One *A = new One[100];
Memory is allocated and 100 constructors are called.
Similarly if you say:
delete[] A;
All 100 destructors are called and memory is deallocated