IsoCpp.org offers a FAQ regarding placement new:
The example they provide is:
#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred
void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2
Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)
// The pointers f and place will be equal
// ...
}
Wouldn't the above code violate C++'s strict aliasing rule since place
and memory
are different types, yet reference the same memory location?
(I know that pointers of type char
can alias any other type, but here we seem to have a void*
aliasing a char*
, which is not allowed from what I understand?)
I suspect that most memory allocators would also violate the strict aliasing rule in a similar manner. What is the proper way to comply with the strict aliasing rule when using placement new?
Thank you