Maximum number of elements in an array vs. maximum indexer value

こ雲淡風輕ζ 提交于 2020-04-12 17:24:25

问题


today I started wondering about something in the MSDN. This article demonstrates, how one can increase the memory allocatable by an array under .NET 4.5 and x64. This is a nice feature, but something in the description provided by Microsoft baffeles me.

Under the section "Remarks" they say, that:

The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types.

Since I mainly have int[] or double[] the latter number is relevant for my indexing. I can create an array with int[] TestArray = new int[2146435071], which is fine. However, under the same section Microsoft states:

The maximum number of elements in an array is UInt32.MaxValue.

Which is (according to the MSDN):

The value of this constant is 4,294,967,295; that is, hexadecimal 0xFFFFFFFF.

Now. If I get that right, I can have an array with up to 4,294,967,295 elements (for example ints) but due to the array being indexed by an int and not an uint I am not able to access the "upper" half of my data?

This confuses me a lot, sice it seems I am missing something essential here.

I hope you can enlighten me

Kind Regards

EDIT:

I understand that I can create multi-dimensional arrays, but an array of length 2e9 an width 2 seems a bit stupid. Aren't multi-dimensional arrays mapped to one-dimensional ones anyway?


回答1:


The maximum index in any single dimension is 2,147,483,591

Remember that arrays can have multiple dimensions, so you could have a 2-D array that has up to 4,294,967,295 items, but each dimension can have a max length of 2,147,483,591.

So you can have a 2,147,483,591 X 2 array, but not a 1,000,000 X 1,000,000 array.



来源:https://stackoverflow.com/questions/21408786/maximum-number-of-elements-in-an-array-vs-maximum-indexer-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!