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

Mark Mitchell mark@codesourcery.com
Wed Feb 3 19:34:00 GMT 2010


Michael Matz 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.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713



More information about the Gcc-patches mailing list