tags:

views:

248

answers:

4

Not that I would ever need to do this, but I want to understand how it works/doesnt work. I googled quite a bit for max length of an array and can't really find anything.

long[] hugeArray = new long[long.MaxValue];

//No Exceptions
Console.WriteLine("Init");

//Overflow Exception
Console.WriteLine(hugeArray.LongLength.ToString());

hugeArray = new long[int.MaxValue];

//OutOfMemoryException
Console.WriteLine( hugeArray.Length.ToString());

And I guess a follow up question would be, if there is a limit and I am initizlizing outside that limit, why no exception when creating only when using? And is this something the compiler should catch?

A: 

The index of an array is an int, not long. So, I guess technically new long[int.MaxValue] would be the maximum. But you could still get the OutOfMemoryException you are seeing if you really do run out of memory. Are you running this on a 32-bit OS? There isn't a set maximum length, other than the index value is an int, not a long.

marcc
+2  A: 

According to SpankyJ, .NET 2.0 at least had a limit of 2 GB per array. 8 bytes (sizeof long) * ~2^31 = 16 gigabytes (not to mention the actual memory required). As for the Overflow, I agree with marcc that is probably because arrays are expected to be int-indexed (see e.g. this method Though I'm a bit uncertain regarding this, as this overload takes an array of Int64 lengths. This may be an extension only available in later versions.

Overall, though, this is mostly a theoretical issue. Anyone actually relying on this is likely doing something wrong.

Matthew Flaschen
+4  A: 

The underlying IL opcodes for arrays deal with IntPtr (known as native int in the IL documentation):

  • newarr creates an array, and accepts an IntPtr
  • ldelem and stelem read and write elements, and they use an IntPtr index
  • ldlen returns the length of an array, and it returns an unsigned IntPtr

I'd expect a theoretical limit on the number of elements of 2^31-1 on 32-bit platforms, and 2^63-1 on 64-bit platforms.

However, I just tried this code on my 64-bit PC, and I got an OutOfMemoryException on the first allocation, so clearly the practical limits are smaller.

Tim Robinson
A: 

Hi,

I'm thinking to the same question for several monthes now and didn"t find answer.

http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/8fad0cca-c0c9-431b-8079-0befe95e915e

Since the previous discussion, I was used to declare an array (vb .net) Dim tabfil(99999999) this worked thousand of time until... today. I didn't change soft, machine, service pack... and know the maximum is Dim tabfil(85983871)

Dropping other process have no effect, the value is always the same. This is for Vistan and Visual Studio Express 2008 but is not the same with XP. So I just understand what drives limits of an array, is it the array by itself, its index, visual studio ??

Olivier57