Search code examples
c++strict-aliasing

Strict aliasing rule violation


In this link from the isocpp.org faq in the example provided, a Fred object is being constructed with placement new to a buffer that is being allocated for another object i.e. for

char memory[sizeof(Fred)]

As I know the strict aliasing rules allows us to do the opposite i.e. for an object of whatever type, we are allowed to have a char* point at it and we can dereference that pointer and use it as we want.

But here in the example the opposite is happening. What am I missing?


Solution

  • Placement-new creates a new object. It doesn't alias the old object. The old object (the char array in this example) is considered to stop existing when the placement-new executes.

    Before placement-new, there is storage filled with char objects. After placement-new, there is storage filled with one Fred object.

    Since there is no aliasing, there are no strict-aliasing problems.