0

This is a purelly theoretical question, so please do not warn me of that in your answers.

If I am not mistaken, and since every array in .NET is indexed by an Int32, meaning the index ranges from 0 to Int32.MaxValue.

Supposing no memory/GC constraints are involved an array in .NET can have up to 2147483648 (and not 2147483647) elements. Right?

2
  • Similar/Dupey question : stackoverflow.com/questions/1391672/… Commented Mar 20, 2011 at 7:53
  • if you find you need an array with more than 2147483647 items, you should probably look for another algorithm... Commented Mar 20, 2011 at 7:54

1 Answer 1

9

Well, in theory that's true. In fact, in theory there could be support for larger arrays - see this Array.CreateInstance signature which takes long values for the lengths. You wouldn't be able to index such an array using the C# indexers, but you could use GetValue(long).

However, in practical terms, I don't believe any implementation supports such huge arrays. The CLR has a per-object limit a bit short of 2GB, so even a byte array can't actually have 2147483648 elements. A bit of experimentation shows that on my box, the largest array you can create is new byte[2147483591]. (That's on the 64 bit .NET CLR; the version of Mono I've got installed chokes on that.)

EDIT: Just looking at the CLI spec, it specifies that arrays have a lower bound and upper bound of an Int32. That would mean upper bounds over Int32.MaxValue are prohibited even though they can be expressed with the Array.CreateInstance calls. However, it also means it's permissable to have an array with bounds Int32.MinValue...Int.MaxValue, i.e. 4294967296 elements in total.

EDIT: Looking again, ECMA 335 partition III section 4.20 (newarr) specifies that a initializing a vector type with newarr has to take either a native int or int32 value. So it looks like while the normally-more-lenient "array" type in CLI terminology has to have int32 bounds, a "vector" type doesn't.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you very much Jon! Of course in practical terms that's not possible, but when code correctness is mandatory one has to deal with this kind of things.
@Miguel: what has "code correctness " got to do with your question?
I need to assure theoretical limits for these kind of things are never exceeded under any circunstance.
@Miguel: Surely you need to make sure that the practical limits are never exceeded... because they're what will actually determine whether code can run or not. "Correct" code which crashes isn't really useful, is it? What's the context here? Does a limit of long.MaxValue help you, if that's the true theoretical constraint rather than int.MaxValue? You should read the CLI spec for more details.
Why is the array overhead 56 bytes for a 2GB array on a 64-bit machine?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.