On 8 February 2018 at 13:57, Ole Kniemeyer wrote: > Hi, > > I have a small code snippet where I'm not sure if it's a GCC bug or undefined behaviour, so maybe > someone can help? The code is > > #include <new> > > void Func(void*); > > struct A > { > A() = default; > A(const A& src) > { > Func(src.x); > } > > void* x; > }; > > void Test(void* ptr) > { > A tmp; > tmp.x = ptr; > new (&tmp) A(tmp); > } > > If you paste this code at gcc.godbolt.org and use -O3, GCC <= 5.X and Clang produce the code which > I'd expect: > > Test(void*): > jmp Func(void*) > > But GCC 6 and 7 call Func with a nullptr: > > Test(void*): > xorl %edi, %edi > jmp Func(void*) > > I admit that the code is a bit dubious because it copy-constructs a new A object with itself and > the copy constructor doesn't copy anything, so is this undefined behavior or a GCC bug? Looks like undefined behaviour to me too. You can't have two objects alive at the same location simultaneously. As soon as the second constructor starts you can no longer access the old object.