Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rectangular array physical in memory order

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]...?

like image 273
Petr Abdulin Avatar asked May 13 '12 13:05

Petr Abdulin


People also ask

How are arrays laid out in memory?

The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.

How are 1d arrays stored in memory?

All the data items of an array are stored in consecutive memory locations in RAM. The elements of an array are of same data type and each item can be accessed using the same name.

Which array is also known as a rectangular array?

A rectangular array of numbers is called a matrix. A matrix with n rows and p columns is referred as an n × p matrix. If a matrix has n rows and 1 column, it is called an n-dimensional column vector.


1 Answers

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).

like image 73
Jon Avatar answered Oct 06 '22 14:10

Jon