Re: delete operator again
RenjithMohan wrote:
I have a seemingly simplistic question. I would like to know why the
array delete operator is not automatically invoked.
Because that would lead to potential inefficiencies.
We all know that the compiler has to do some bookkeeping in order to
free the entire block of memory.
That premise is wrong: there certainly are allocators that do not need
to store the size of allocated memory anywhere. And those who do might
not store the information "adjacent" to the allocated memory.
There was a dispute in the standard
whether the size of the array must be specified in the [] syntax.
Finally it was decided that since it is error prone, it should be
avoided.
If that is the case why cant the syntax be further simplified and
automatically invoke the correct form of the delete operator?.
I'd rather like to ask you why you user operator new [] in the first
place. Why not use std::vector. This is better in almost all metrics
used.
[snip]
You could of course cook up more complicated scenarios like array of
polymorphic types etc. But the point remains the same . It is just a
race between the normal form and array form of delete of the type.
Since the nature of the type is always given why cant the compiler
guess to call the correct form ?.
If you dont mind the overhead, a better question would be: why not just
always use operator new[] and thus always use operator delete[]? Of
course the simpler way would be to use std::vector instead (and perhaps
some template-class in case std::vectors overhead of two pointers gets
to expensive).
/Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]