Type* ptr = new (void_ptr) Type( args... )
creates an instance of an object.
Without doing this, or something similar, there is no object to interact with there. Even if there are no methods, no constructors, and it is just a plain struct; without an object, using pointers-to-memory as if it pointed to an object is not generally legal in C++.
The abstract machine that C++ is specified in terms of has concepts that tend not to be compiled into assembly instructions. Among them is "is there an object there". The standard is carefully written such that "is there an object there" doesn't require any runtime state.
But the compiler is free to track where there are objects, and presume that when you interact with memory via a pointer you are interacting with an actual instance of an actual object.
This rules out a certain thing called "aliasing", where you have pointers to two kinds of objects, and you modify one, and the other one changes; aliasing (in general) isn't allowed in C++. This permits compilers to assume that when you edit what a double*
points to, a int
nearby doesn't have its value changed; there is no legal way for there to be both an int
and a double
at the same spot, and a double*
can only be written to if it points at a double
.
Similarly, a buffer of raw bytes? Guaranteed not to be a mystruct
, at least until someone creates a mystruct
there. What more, when a mystruct
is created, the values of its members are guaranteed to be indeterminate, and when it is destroyed, any unobserved changes to the values of its members can be presumed never read by anyone.
That permits yet more optimizations, as useless writes are discarded and reads optimized away.
All of this breaks down horribly when you violate the aliasing rules of C++. These break downs only occur sometimes and in response to seemingly unrelated changes often. Many programmers thus ignore the aliasing rules, and entire code bases are compiled with flags disabling aliasing rules.