Allocating a single object larger than 2GB using new in C++ (on Windows)

Add const to your declaration:

const uint64_t sz = 1LLU << 32;

and you'll get the same error:

Compiler Error C2148

This is a 'safety' MSVC threshold for an array allocation, but since you give non-const size variable the compiler does not resolve it at compile-time.


You definitely found a compiler bug (targetting x64) and you should submit it to microsoft.

It seems that whenever the compiler knows the actual size to allocate an array to be greater_equal than 2^33 it will wrongfully 'optimize' the size to 0. Since allocating a 0-size array is perfectly valid ,you'll get invalid access when accessing it beyond some index (the allocated array will occupy some memory).

bug reported: https://developercommunity.visualstudio.com/content/problem/779749/msvc-2019-erroneously-replaces-known-arraysize-gre.html

BTW: std::array<> doesn't suffer from it ,it seems.