Re: Array new followed by non-array delete - request for experience
int19h@gmail.com () wrote (abridged):
I would like to ask everyone who had previously had issues with non-
array operator delete used to delete the result of array new, i.e.:
[...]
I know that it is undefined behavior according to the Standard.
What I would like to know, however, is how strict is this rule
in practice.
With MS VC++ it depends on whether the type has a destructor. If it has,
the array new expression stores a count of the number of objects in the
array, and the array delete expression uses that count to destroy them.
The non-array new expression does not store the count, so if you use the
wrong delete expression you are very likely to get a crash.
Types without destructors, like char or int, seem to work. For these the
compiler is quite forgiving. Neither version of the new expression stores
the number of objects. You could probably allocate with new or new[] and
delete with free(). (I've not actually tested that.)
I don't know about other compilers.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]