Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum array dimension like a[1][1][1][1]....[1] in C#

Tags:

arrays

c#

.net

I just saw this question and infact tried to answer it as well. But while answering I thought what could be the answer in case of C#? I am looking for some MSDN doc or any relevant source like in Java

What is the maximum dimension allowed for an array in C# like a[1][1][1][1]....[1]. I tried to search on SO but was not able to find.

The best I got up to was that "An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing."

Also I am aware of the fact that Maximum Size that an Array can hold is

System.Int32.MaxValue

Kindly do let me know if this is a duplicate I will delete my question.

like image 977
Rahul Tripathi Avatar asked Mar 28 '14 13:03

Rahul Tripathi


People also ask

What is the maximum dimension of an array in C?

There is no fixed limit to the size of an array in C. The size of any single object, including of any array object, is limited by SIZE_MAX , the maximum value of type size_t , which is the result of the sizeof operator.

What is the maximum dimensions of an array?

Although an array can have as many as 32 dimensions, it is rare to have more than three. When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.

How do you find the maximum of an array?

Initialize max with the first element initially, to start the comparison. Then traverse the given array from second element till end, and for each element: Compare the current element with max. If the current element is greater than max, then replace the value of max with the current element.

What is the maximum size of 2D array?

C++, Using 2D array (max size 100*26) - LeetCode Discuss.


1 Answers

The limit is 32. I checked it with the code:

var b = Array.CreateInstance(typeof (int), new int[32]);
var a = Array.CreateInstance(typeof (int), new int[33]);

It applies for both 64 and 32-bit targeted applications.

Actually, I needn't have googled it. Straight from MSDN:

An element is a value in an Array. The length of an Array is the total number of elements it can contain. The rank of an Array is the number of dimensions in the Array. The lower bound of a dimension of an Array is the starting index of that dimension of the Array; a multidimensional Array can have different bounds for each dimension. An array can have a maximum of 32 dimensions.

About jagged arrays

However, I just noticed that the syntax you used in your edit (and also the syntax Peter used in the comments below) isn't how multi-dimensional arrays are defined in C#. Multi-dimensional arrays are defined as:

int[,,,] arr = new int[0,0,0];

And they are arrays of regular integers, that happen to have multiple dimensions. The syntax:

int[][][] = new int[0][][];

Defines something that in C# is called a jagged array. Jagged arrays are just regular, single-dimensional arrays that happen to have other arrays as elements. You can have multi-dimensional arrays of jagged arrays, or the other way around, as the MSDN article points out.

They are treated differently, however. Jagged arrays are just arrays of (effectiely) regular* objects, and as such, aren't regulated in the same way as multi-dimensional arrays. There is no theoretical limit to the number of "dimensions" a jagged array can have, and in fact the CLR happily let me run the following code:

Array arr = new int[0];
for (int i = 0; i < Int32.MaxValue - 1; i++) {
    var newArr = Array.CreateInstance(arr.GetType(), 1);
    newArr.SetValue(arr, 0);
    arr = newArr;
}

Right up until my system became completely unresponsive and crashed. No exception was thrown.

Limiting the loop to 1000 iterations, you find that the type of the resulting array is:

System.Int32[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]...

In addition, although I can't find a source for this, the number of dimensions a jagged array can have probably isn't even limited by the single object memory limitation, because each array only has references to objects. For example, a jagged array of (eventually) Int32 with a nesting factor of, say, 1000 and 2 elements per array, would take up a negligible amount of memory (that is, on its own), but all the arrays combined would take up an exponential amount.

* With a little bit of syntax sugar. A more syntactically correct way of encoding jagged arrays would be: int[][] arr = new int[][0]; because int[] is the element type.

It's implementation defined

The number of dimensions an array can have appears to be implementation-defined. The number doesn't appear anywhere in the CLI specification, and the code provided by Peter below runs fine on Ideone's mono-2.8, where it appears the limit is 255.

like image 161
GregRos Avatar answered Oct 05 '22 19:10

GregRos