RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v3]
Kim Barrett
kbarrett at openjdk.org
Thu Feb 19 21:52:40 UTC 2026
On Thu, 19 Feb 2026 15:56:42 GMT, Leo Korinth <lkorinth at openjdk.org> wrote:
>> test/hotspot/gtest/utilities/test_globalDefinitions.cpp line 354:
>>
>>> 352: static_assert(sizeof(sizeof_auto(char[std::numeric_limits<uint16_t>::max() + 1LL])) == sizeof(uint32_t));
>>> 353: #if defined(_LP64) && !defined(_WINDOWS)
>>> 354: // char array sometimes limited to 2 gig length on 32 bit platforms (signed), and on windows (even 64 bit!!!)
>>
>> Does it work to have an array of some larger type (like a struct with a bunch of members)
>> to get a larger sizeof? Or is it that somehow the array byte size is limited? The latter would
>> seem pretty surprising to me, but I don't know windows at all well.
>
> It is a strange limitation.
> this works: `const char* c = new char[3'000'000'000];`
> this does not work: `sizeof (char[3'000'000'000]);`
>
> I get:
>
> error C2148: total size of array must not exceed 0x7fffffff bytes
> error C2070: 'char [-1294967296]': ill-formed sizeof operand
>
>
> I think the compiler confuses `sizeof` expressions with limitations of static arrays. Which is extra confusing because there is no problem allocating the array dynamically with `new`.
Perhaps add to the comment a mention of MSVC compiler error C2148. Something like
"Disabled for Windows because of compiler error C2148."
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2830325717
More information about the hotspot-dev
mailing list