I am trying to understand placement new-expressions in C++.
This Stack Overflow answer states that T* p = new T(arg); is equivalent to
void* place = operator new(sizeof(T)); // storage allocation
T* p = new(place) T(arg); // object construction
and that delete p; is equivalent to
p->~T(); // object destruction
operator delete(p); // storage deallocation
Why do we need the placement new-expression in T* p = new(place) T(arg); for object construction, isn’t the following equivalent?
T* p = (T*) place;
*p = T(arg);
The first thing to note is that
*p = T(arg);is an assignment, not a construction.Now let's read the standard ([basic.life]/1):
For a general type
T, initialization could have been completed if placementnewwere used, but that's not the case. Doingdoesn't start the lifetime of
*p.The same section reads ([basic.life]/6):
operator=is a non-static member function and doing*p = T(arg);, which is equivalent top->operator=(T(arg)), results in undefined behaviour.A trivial example is a class that contains a pointer as a data member that is initialized in the constructor and is dereferenced in the assignment operator. Without placement
newthe constructor won't be called and that pointer won't be initialized (complete example).