segmentation violation due to unnoticed invalid code ?
Nathan Sidwell
nathan@acm.org
Wed Apr 14 07:42:00 GMT 1999
Alexandre Oliva wrote:
>
> On Apr 13, 1999, Luc.Maisonobe@cnes.fr (Luc Maisonobe) wrote:
>
> > A *a = new A [1];
> > delete [] ((void *) a);
>
> When you new an array, you must delete[] it using a pointer of the
> actual type of the array elements. Since A is not void, the behavior
> is undefined.
> > Of course this is weird code (it has been corrected since then), but I
> > would suggest egcs should complain about freeing an anonymous pointer.
>
> Yep, it would probably be a nice feature, since `delete[] (void*)foo'
> can *never* be well-defined behavior, as `new void[n]' is ill-formed.
I believe both `delete (void *)e' and `delete[] (void *)e' to be undefined. The
former is because the static type (void) must be different to the dynamic type
of the object (fails first part of 5.3.5/3) The latter is also undefined
because of the second part of 5.3.5/3 as footnote 73 notes.
So, we could warn on both instances. I guess there might be some code using
`delete e' to deallocate random blobs of memory, but shouldn't they use
`operator delete(e)' instead?
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
You can up the bandwidth, but you can't up the speed of light
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
More information about the Gcc-bugs
mailing list