Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating large arrays; OutOfMemoryException VS OverflowException

Tags:

c#

.net

Consider the following:

long size = int.MaxValue;
long[] huge = new long[size];     // throws OutOfMemoryException
long[] huge = new long[size + 1]; // throws OverflowException

I know there is a 2GB limit on the size of a single object, which explains the first exception, but why do I get a different exception once the number of elements surpasses 32bits?

(I am using a 64-bit computer if that's important).

EDIT: I can also define and use an indexer that accepts a long with no problems:

internal sealed class MyClass
{
   public object this[long x]
   { 
      get
      {
         Console.WriteLine("{0}", x);
         return null;
      }
   }
}

...

long size = int.MaxValue;
MyClass asdf = new MyClass();
object o = asdf[size * 50]; // outputs 107374182350
like image 663
Dave Cousineau Avatar asked Jun 08 '12 09:06

Dave Cousineau


2 Answers

C# arrays are indexed by System.Int32. Since size + 1 is beyond Int32.MaxValue, you get an integer overflow.

Use the overload of Array.CreateInstance that takes a long instead, if you really want to use a long as index.

like image 119
user703016 Avatar answered Oct 24 '22 00:10

user703016


So from what I've gathered, something like the following is happening here:

  • Indexers in general could use any type of parameter.
  • The built-in array indexers can accept any integral type...
  • But the underlying implementation of the built-in array indexers requires a value that is <= Int32.MaxValue and will throw an overflow exception for a value that exceeds Int32.MaxValue.

While the latter point feels like some kind of weird contradiction (accepting types larger than Int32, but throwing an exception if you happen to actually use any of those extra bits), it is apparently a side-effect of the fact that some of this is half-implemented for the future implementation of arrays which will be allowed to have more than Int32.MaxValue elements.

like image 43
Dave Cousineau Avatar answered Oct 23 '22 23:10

Dave Cousineau