I need to construct a large dynamically-sized array of type T
, which has an all-zero bit pattern as the representation of the default-constructed value. Actually default-constructing every element like std::vector
does (effectively memsetting the memory to zero) incurs a measurable overhead in my application.
I would like to use calloc
to make use of OS-zeroed pages instead. I cannot simply cast the returned memory to T*
, since no T
was ever constructed in that memory, violating the strict-aliasing rule and causing undefined behavior (see this answer).
Using placement-new in calloced memory is correctly optimized away by Clang 9 and gcc-trunk, but not by released versions of GCC or any version of MSVC (see Godbolt).
Is there any way to reliably get rid of the memset here?