Arrays of arrays

Arrays of arrays

Rate This
  • Comments 48

Most people understand that there’s a difference between a “rectangular” and a “ragged” two-dimensional array.

int[,] rectangle = {
  {10, 20},
  {30, 40},
  {50, 60} };
int[][] ragged = {
  new[] {10},
  new[] {20, 30},
  new[] {40, 50, 60} };

Here we have a two-dimensional array with six elements, arranged in three rows of two elements each. And we have a one-dimensional array with three elements, where each element is itself a one-dimensional array with one, two or three elements.

That’s all very straightforward. Where things get brain-destroying is when you try to make a ragged array of two-dimensional arrays.  Quick, don’t think, just answer.

int[,][] crazy;

What is the type of crazy?

Option one: It’s a one-dimensional array, each element is a two-dimensional array of ints.
Option two: It’s a two-dimensional array, each element is a one-dimensional array of ints.

OK, now that you have your snap answer, think about it carefully. Does your answer change?

I’m not going to tell you the answer just yet. Instead let’s explore the consequences of each possibility.

Consequence One

Surely the way you make any type into an array is to append [] to the type specification, right? But in option two, you stick the [,] into the middle.

Option two is weird. Option one is sensible. 

But wait. If [,][] means "a 1-d of 2-d's", then the order you read it off the page opposes the order you say it -- it looks like two-d-thing-followed-by-one-d-thing, so why shouldn't it read "a 2-d of 1-d's"?

But then why does the "int" come first? By that logic it should come last.

Argh. Maybe option one isn't entirely sensible. Clearly something is not quite perfect with both options. Oh well. Let's move on.

Consequence Two

Now suppose that you wanted to obtain a value in that array, assuming that it had been initialized correctly to have plenty of elements everywhere we need them. How would you do it?

Suppose we’re in option one. It’s a one-d array. Therefore crazy[10] is a two-d array. Therefore crazy[10][1, 2] is an int.

Suppose we’re in option two. It’s is a two-d array. Therefore crazy[1,2] is a one-d array. Therefore crazy[1,2][10] is an int.

Option one is weird -- crazy is of type int[,][] but you dereference it as [10][1,2]! Whereas option two is sensible; the dereferencing syntax exactly matches the ordering of the type name syntax.

Consequence Three

Now suppose you want to initialize the “outer” array but are going to fill in the “ragged” interior arrays later. You’ll just keep them set to null at first. What’s the appropriate syntax to initialize the outer array?

Suppose we’re in option one. It’s a one-d array. Therefore it should be initialized as crazy = new int[,][20]; right?

Suppose we’re in option two. It’s a two-d array. Therefore it should be initialized as crazy = new int[][4,5]; right?

Option two is weird. We said int[,][] but initialized it as [][4,5]. Option one is sensible.

What C# actually does

It’s a mess. No matter which option we choose, something ends up not matching our intuition. Here’s what we actually chose in C#.

First off: option two is correct. We force you to live with the weirdness entailed by Consequence One; you do not actually make an element type into an array of that type by appending an array specifier. You make it into an array type by prepending the specifier to the list of existing array specifiers. Crazy but true.

This prevents you from having to live with any weirdness from Consequence Two; in this option, the dereferencing happens with the same lexical structure as the declaration.

What about Consequence Three? This one is the real mind-bender. Neither choice I offered you is correct; apparently I’m a sneaky guy. The correct way to initialize such an array in C# is:

crazy = new int[4,5][];

This is very surprising to people!

The design principle here is that users expect the lexical structure of the brackets and commas to be consistent across declaration, initialization and dereferencing. Option two is the best way to ensure that if declaration has the shape [,][] then the initialization also has that shape, and so does the dereferencing.

That all said, multidimensional ragged arrays are almost certainly a bad code smell. Hopefully you will never, ever have to use your new knowledge of these rules in a production environment.

Life is much better if you can instead use generic collections. It is completely clear what List<int[,]> means – that’s a list of two-dimensional arrays. Whereas List<int>[,] means a two-d array of lists of int.

  • Oh my goodness, that is very weird indeed!

    I would have expected it to behave as you outlined in option 1.  I've never had to use multidimensional ragged arrays in C#, so I haven't ran across this "problem".  

    Was your holiday good?  Are you finished with the pre-recorded posts now?  Did you see any Beaver-Sharks??? :-)

  • Enlightening, as always... thanks !

  • It's interesting to note that this isn't a problem for pointers, because you cannot have a pointer to an array. So all the weird cases there are disabled anyway:

       int*[] a;  // array of pointers

       int[]* a;  // error

       int[]*[];  // error

       int*[]*;   // error

    Anyway, the array declarator problem is something that always bugged me as awfully inconsistent, but then you can only do so much with the legacy C/C++/Java syntax. It's interesting to see how other languages from the family deal with the problem.

    Java simply weasels out. It doesn't have multidimensional arrays, nor pointers, so the only thing you can stick the [] declarator on is the base type, or another array declaration. Obviously, when they're all 1-dimensional, it doesn't matter which side you read it from, left or right - it's all the same:

       int[][] a;

    D 1.0 has much more to deal with, since it has static arrays a la C (with size specifier), and also pointers to arrays etc. This can quickly become a mess, so to retain some consistency, they go for "option 2", and read type modifiers consistently from right to left. So:

      int[3][4];       // array of 4 arrays of 3 ints

      int*[]*[3] a;  // array of 3 pointers to dynamic arrays of pointers to int

    Which is of course inconsistent with usage, since the order is reversed:

      *((*(a[1]))[2]) = 123;

    To mitigate that somewhat, D 1.0 also gives the option of declaring arrays C-style, with brackets following variable. For those declarations, the order is left-to-right, and thus consistent with C/C++:

      int a[4][3];       // array of 4 arrays of 3 ints

      int* (*a[3])[];  // array of 3 pointers to dynamic arrays of pointers to int

    This is somewhat consistent in that if you want the declaration to match usage, you have to go all the way, and match the placement of [] in the declaration with usage as well (i.e. after the variable). But then two subtly different ways to do the same thing are a recipe for bugs, and D language reference itself recommends always sticking to the first, prefix form.

    Personally, I've always found this to be the area where Pascal-derived languages shine - especially Modula-2. You can't beat the clarity of:

      VAR a : ARRAY[1..3] OF POINTER TO ARRAY[1..10, 1..10] OF INTEGER;

    Pascal/Delphi are somewhat less readable (but more concise) when it comes to pointers, but the general idea is the same: types should _always_ be read left to right in a natural way. Of course, if you do this, you want the type to be on the right of variable name in a declaration, otherwise it just looks weird. I mean, say we had:

       [,][]int a;  // huh?

    I believe D 2.0 explored that at some point, but then backed away to the same behaviour as D 1.0. I can understand why - I'll take the existing C# syntax any day, thank you very much...

    By the way, it seems that C++0x might get Pascal-style declaration syntax as well. In the minutes of July 2009 WG21 meeting (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2920.html), there is this interesting bit:

    "Crowl, Stroustrup, and Vandevoorde will work on a Linear Declaration Syntax that is an alternative to and generalization of Unified Function Syntax. Some examples:

    [] p1: *const int; // Pointer to const int

    [] p2: *int const; // Error

    [] p3: (char)->*int; // Function talking a char returning pointer to int

    [] p4: *()->int; // Pointer to function returning an int

    [] p5: ()->*()->int; // Function that might return p4

    [] p6: [2][3] * [4] int;// Array of 2 arrays of 3 pointers to arrays of 4 ints"

    Of course, for C++ this is really long overdue because of the complexity of its type system, and the fact that declaring something like a pointer to array is a fairly common thing. In contrast, I do not remember the last time I had to write a multidimensional array in C#...

  • I have avoided multi-dimensional arrays for years now.  They are weaker than "ragged" arrays in most ways.  The most important to me is speed - getting or setting rectangle[1,2] is about twice as slow as ragged[1][2].

  • That's enough to drive anyone crazy.  And people wonder why it's so hard to understand code written by someone else.

  • The other fun part is that System.Type follows Option 1. So

       typeof(System.Int32[,][]).Name =="System.Int32[][,]"

    Someone recently filed a bug on the XAML parser because of this (we write out the type name using System.Type.Name), I had some fun explaining why it was By Design.

  • Avoid ragged arrays and multi-dimensional ones with 3 or more subscripts.   This 'too smart for your own good' code has bitten me several times usually at the expense of multple days of debuger fun.

  • OK, I completely understand why C# language implementers care about this, so props to you for posting it. But for anyone else, the answer is, "It doesn't matter; just don't do it!"

  • Actually, I didn't find this weird or mind-twisting - option 2 makes perfect sense.

    If we mentally visualize the one-dim ragged array as a histogram chart, where the ragged array size represents the bar height, then the int [,][] crazy is just like one of those really annoying isometric 3-D histograms with square bars arranged in a grid of N x M rows and columns and still each ragged array represents different bar heights.

  • @DRBlaise,

    Getting an element from a ragged array takes two accesses, while getting an element from a multi-dimensional array only takes one access. So multi-dimensional arrays should usually be faster, not slower.

  • One to file under "things I hope I never have to worry about"

  • Devil is in the detail, huh? :-)

    It's all well and good for us who USE the language to say "just don't do it", but the problem is, those who DESIGN the language cannot afford to overlook ANY possible syntactically valid use of the language they design, no matter how silly and impractical it looks (and I am NOT saying that arrays of arrays are silly and impractical: all I am saying is that even if they were, they still must work, full stop).

    Which brings us to an interesting question: how do you unit-test a language? Is it possible to produce the sufficient amount of code equivalents of human-language tongue-twisters to make sure the language is complete? How do you catch the programming-language equivalents of the English phrase "we eat what we can and what we can't eat, we can," where you're not sure what they said: that "what they couldn't eat, they could," or that "what they couldn't eat, they canned?" Ok, we humans can intuitively figure out these things, but computers don't have any intuition yet...

  • @commongenius,

    Unfortunately, it doesn't work that way. 0-based 1D arrays have special IL opcodes to instantiate and access elements, and those are efficiently JIT-compiled, with all those nice optimizations such as eliding unnecessary bound checking in a loop. For multidimensional arrays, element access is implemented via method calls - just check the IL. And those calls are slow.

    That kills perf on the spot. All benefit you get from locality is lost from method access overhead - a 2D array is roughly 1.5 times slower than a corresponding jagged array because of that.

  • What's wrong with int[][][] sane; ?

  • @Pavel Minaev

    You are correct in saying that there are no special IL opcodes for multi-dimensional arrays, but that's no reason for it to be slow.

    The JITter could produce special inline native code for method calls to the Array object that get/set multi-dimensional arrays.

    The problem (so far as I know, and I could easily be out of date) is that the current JIT makes no attempt to do this. Theoretically this should be faster than jagged array access.

Page 1 of 4 (48 items) 1234