1

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?

7
  • You can start with the specification, though I don't have time to check if it answers your specific question: learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented Aug 16, 2022 at 4:05
  • I expect this (result) tells you something about how they are structured in memory though. Commented Aug 16, 2022 at 4: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. Commented Aug 16, 2022 at 13:57
  • @Craig No it's row-major in SAFEARRAY also learn.microsoft.com/en-us/openspecs/windows_protocols/ms-mcis/… see the last paragraph Commented May 12, 2023 at 11:36
  • 2
    @Craig Sorry looks like you're right, misunderstood Commented May 12, 2023 at 13:26

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It's good to know it's in the spec and not just an implementation detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.