This is the mail archive of the
mailing list for the GCC project.
Re: [PATCH] Fix C++ strict-aliasing issues with memcpy folding
On Wed, 3 Feb 2010, Gabriel Dos Reis wrote:
> >> > If the placement new to part of an object makes the object go away, a
> >> > delete of it would be undefined (5.3.5/2). ÂHence I wouldn't be able to
> >> > deallocate storage in the above example with a 'delete p' at the end. ÂI
> >> > wouldn't be able to deallocate it with 'delete f' either (to see this
> >> > replace the above placement new with 'f = new (&p->mem) float;'
> >> > making f point into the middle of the storage of *p).
> >> >
> >> > This surprising effect doesn't depend on the declared storage type, it
> >> > breaks just as well if X.mem would be a char.
> >> Sure, that's true. ÂBut, since char is special, there's no need for my
> >> as-if. ÂIn the case of char, you are permitted just to splat a new
> >> type on type of the storage.
> > Hmm, I'm not sure I'm making myself clear. ÂI am wondering about the
> > impossibility to delete such storage after placement new did its work,
> > char or not, if placement new implicitely destroys the surrounding
> > object.
> Hmm, I'm having trouble seeing the impossibility.
> delete p;
> does two things
> (1) run a destructor for the objected pointed to by 'p';
> after which, the object ceases to exist.
> (2) return storage back to the system
> which of (1) or (2) appears impossible?
5.3.5/2 says that in 'delete p', if it's an object delete (not array
delete), then p must be either: null, pointing to an object, or a base
subobject of an object.
Now, if you say (as you did upthread) that object *p ceased to exist with
the placement new, then p doesn't point to an object anymore, hence
'delete p' is undefined.