Why is Array.MaxLength in .NET 2147483591 (0x7FFFFFC7) instead of Int32.MaxValue? #121495
-
|
In .NET, the
Can someone with knowledge of the CLR internals explain why the maximum array length is 2147483591 instead of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
This is just to help avoid issues with integer overflow, as per: runtime/src/coreclr/vm/gchelpers.cpp Lines 604 to 609 in 02bea82 There is various logic that needs to do offset and other calculations using the number of elements and a slightly smaller limit helps avoid potential issues, making typical code more robust without being limiting. Notably, allocating such large arrays, regardless of what the actual runtime limit is, has other fundamental problems and is typically not recommended. |
Beta Was this translation helpful? Give feedback.
This is just to help avoid issues with integer overflow, as per:
runtime/src/coreclr/vm/gchelpers.cpp
Lines 604 to 609 in 02bea82
There is various logic that needs to do offset and other calculations using the number of elements and a slightly smaller limit helps avoid potential issues, making typical code more robust without being limiting.
Notably, allocating such large arrays, regardless of what the actual runtime limit is, has other fundamental problems and is …