[PATCH] Fix C++ strict-aliasing issues with memcpy folding

Michael Matz matz@suse.de
Thu Feb 4 13:24:00 GMT 2010


Hi,

On Wed, 3 Feb 2010, Mark Mitchell wrote:

> >>> struct X { int count; char mem[8]; } *p;
> >>> p = new X;
> >>> ...                                  // e.g. init all members of *p
> >>> float *f = new (&p->mem[0]) float;
> >>> *f = 1.0;
> >>>
> >>> Is it allowed to access p->count after this assignment to float?
> >> If we make the special exception for arrays of characters
> >> (which means 'just storage to hold objects'), then the
> >> access to p->count is OK.
> 
> My feeling is that p is still valid, and still points to an X in this
> example.  The placement new is valid, since it is being done on array of
> characters.  (We can argue whether it's OK if it's "int mem[2]", but
> let's ignore that for the moment.)  So, I think p->count is still valid
> here, and, in fact, so is delete p.
> 
> This all depends on the fact that mem is an array of characters.  An
> array of characters is always (in my model) understood to be a blob of
> bytes in which you can create and destroy other objects.  The fact that
> it's a non-static data member of X is not important.  My feeling is that
> an array of characters is implicitly a union of the array of characters
> and whatever type is created via placement new.

Okay, if this was the intent, and the language in the standard is modified 
to say exactly this pretty clearly, then all the better.


Ciao,
Michael.



More information about the Gcc-patches mailing list