0

I have a few questions about how .Net stores multi-dimensional arrays in memory. I am interested in true multi-dimensional arrays not jagged arrays.

How does the Common Language Runtime (CLR) store multi-dimensional arrays? Is it in row-major, column-major, Iliffe vector or some other format?

Is this format required by the Common Language Infrastucture (CLI) or any other specification?

Is it likely to vary over time or between platforms?

andypea
  • 1,343
  • 11
  • 22
  • You can start with the specification, though I don't have time to check if it answers your specific question: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/arrays – ProgrammingLlama Aug 16 '22 at 04:05
  • I expect [this](https://stackoverflow.com/a/797383/3181933) ([result](https://rextester.com/CQOVE99093)) tells you something about how they are structured in memory though. – ProgrammingLlama Aug 16 '22 at 04:07
  • I wouldn't expect it to vary over time given the convergence on C-heritage languages. Note that for COM interop, to my knowledge, COM SafeArrays are stored column-major not row-major (as COM followed classic VB which followed Fortran in this scheme). I would expect that the .NET built-in marshaling would normally take care of this. – Craig Aug 16 '22 at 13:57
  • @Craig No it's row-major in SAFEARRAY also https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-mcis/3aec369a-beb8-407f-995e-ee4d94666ea4 see the last paragraph – Charlieface May 12 '23 at 11:36
  • @Charlieface The sequence shown in the last paragraph is exactly what I would expect for column-major, not row-major. The example is organized in memory as c1r1, c1r2, c2r1, c2r2, ... so that the natural addition without completely re-organizing the array is to add another column. – Craig May 12 '23 at 13:18
  • I would expect in .NET, the layout would be r1c1, r1c2, r2c1, r2c2, ... – Craig May 12 '23 at 13:25
  • 2
    @Craig Sorry looks like you're right, misunderstood – Charlieface May 12 '23 at 13:26

1 Answers1

3

This is in the specification ECMA-335 Partition I (my bold)

8.9.1 Array types

Array elements shall be laid out within the array object in row-major order (i.e., the elements associated with the rightmost array dimension shall be laid out contiguously from lowest to highest index). The actual storage allocated for each array element can include platform-specific padding. (The size of this storage, in bytes, is returned by the sizeof instruction when it is applied to the type of that array‘s elements.)

Section 14.2 also has more explanation.

These two sections specifically refer to arrays as opposed to vectors, the latter of which is the more familiar zero-based one-dimensional array used in most places.

Arrays on the other hand, can be multi-dimensional and based on any bound, they can also be one-dimensional.

So essentially, it's just one big array under the hood, and uses simple math to calculate offsets.


"Is it likely to vary over time or between platforms?" I'll get my crystal ball out... The spec can always change, and implementations may decide to derogate from it.

Charlieface
  • 52,284
  • 6
  • 19
  • 43