This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On Tue, 2 Feb 2010, Gabriel Dos Reis wrote:

> > Just to be crystal clear: if that storage was anonymous,  do you agree that
> > we can construct arbitrary types into it? ÂI.e. is the following valid in
> > C++? :
> >
> > struct X { int mem[2]; } *p, *q;
> > float *f;
> > p = new X; q = new X;
> > f = new (&p->mem[0]) float;
> this assumes that the storage is properly aligned, etc.

Right, let's assume this.

> After the placement new, the object previously pointed
> to by 'p' ceases to exist.
> > *f = 1.0;
> OK.
> > *q = *p;
> The reads invokes undefined behaviour: if there is any
> object at all pointed to by 'p' it never was initialized.

Would it make a difference to you, if I had these two inits in front of 
the placement new?:

p->mem[0] = 0; p->mem[1] = 0;

> > (incidentally it's interesting to think about what type the access '*p'
> > has)
> '*p' is lvalue of type 'X'.

Although a part of it (the space in mem[0] at least) is a float?  And type 
'X' doesn't contain a float?  That's surprising to say the least :)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]