Original post by HnefiOr, safer code:char mem[sizeof(PMemory)];void* pBuff = mem;PMemory* pMemory = ::new(pBuff)PMemory();
Safer? It looks like a textbook example of brutal stack smashing: if "pMemory" is copied back to the caller, the stack allocated "mem" goes out of scope and the object overlaps with arbitrary subsequent stack frames.
An allocated buffer, at worst, can only be leaked.
The only safe thing about this variant is that allocation "cannot fail" (unless the stack overflows, of course), but checking the result of malloc or operator new is more practical.