Rectangular array physical in memory order

2020-02-13 08:04发布

问题:

In C, in array say A[2][3] rightmost index change resulted in a smallest memory address shift, i.e. elements were located in memory as A[0][0], A[0][1]...

Is same is true for rectangular arrays in .NET? If we have, say array a[2, 3] are elements located in memory as a[0, 0], a[0, 1]...?

回答1:

The CLI specification, section 8.9.1, states:

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.

So the answer is yes -- you will first encounter all elements of the first row, then all elements of the second row, etc (as the spec says, this is called row-major order).