Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.Copy always row-major?

From the MSDN documentation on the Array.Copy method:

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).

I have always assumed that, in C#, two-dimensional arrays are laid out in row-major order (and similarly for higher dimensions), so I'm confused about the meaning of the parenthesized “or columns” in the above documentation. Does it mean that there may be circumstances where the .NET Framework uses column-major ordering?

like image 571
Douglas Avatar asked Jun 10 '13 13:06

Douglas


People also ask

Is row-major or column major faster?

It only shows that in C language, 2-D arrays are stored in row major order and thus iterating its elements in a row major order is more efficient. In languages like Pascal and Fortran, iterating by column major order will be more efficient because 2-D arrays are stored in column major order there.

What does it mean for an array to be row-major?

The elements of an array can be stored in column-major layout or row-major layout. For an array stored in column-major layout, the elements of the columns are contiguous in memory. In row-major layout, the elements of the rows are contiguous. Array layout is also called order, format, and representation.

Are C arrays row-major?

Row-major order is used in C/C++/Objective-C (for C-style arrays), PL/I, Pascal, Speakeasy, and SAS.

Why is column major faster?

Accessing a column-major array in the column-row order will be efficient because the array is stored sequentially in this manner (and the CPU pre-fetches data required next). In the figure above this means the data would be accessed as 1, 4, 7.


1 Answers

I suspect it is merely trying to convey that an array is not a rectangle - it is a single linear space. Any concept of "row" or "column" is actually the invention of the user. There are no rows and no columns; any convention along the lines of arr[x,y] is "row x, column y" or "column x, row y" is purely that: a convention; part of our imagination in conceptualizing something. The only real order is which index is navigated first. The first? or the last?

Whether you call that "rows" or "columns" is up to you, and there are examples of both in use in the wild.

like image 155
Marc Gravell Avatar answered Oct 04 '22 14:10

Marc Gravell