I think this post is missing that one cannot write a memory allocator that returns objects of different types in conforming/correct C. I.e. you can't write malloc because it violates the type aliasing restrictions.<p>This can be worked around by putting the allocator in a separate object file (no LTO) or shared library, or by annotating it with compiler attributes that locally switch off aliasing, or by hoping the compiler doesn't exploit the UB inherent in the allocator to your ruin.<p>Same game in C++ as of the last time I checked, but at least there's placement new and std::launder as workarounds, which strictly speaking need to be used when creating objects with malloc too.<p>> no actual replacement occurring, rather a simple recasting, since both structs have the same memory footprint<p>Can't do that either I'm afraid for much the same reason, need to memcpy over the memory and trust the compiler to elide said copy