Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Array (data structure)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Compact layouts=== {{Main|Row- and column-major order}} Often the coefficients are chosen so that the elements occupy a contiguous area of memory. However, that is not necessary. Even if arrays are always created with contiguous elements, some array slicing operations may create non-contiguous sub-arrays from them. [[File:Row_and_column_major_order.svg|thumb|upright|Illustration of row- and column-major order]] There are two systematic compact layouts for a two-dimensional array. For example, consider the matrix :<math>A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}. </math> In the row-major order layout (adopted by C for statically declared arrays), the elements in each row are stored in consecutive positions and all of the elements of a row have a lower address than any of the elements of a consecutive row: : {| class="wikitable" |- | 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 |} In column-major order (traditionally used by Fortran), the elements in each column are consecutive in memory and all of the elements of a column have a lower address than any of the elements of a consecutive column: : {| class="wikitable" |- | 1 || 4 || 7 || 2 || 5 || 8 || 3 || 6 || 9 |} For arrays with three or more indices, "row major order" puts in consecutive positions any two elements whose index tuples differ only by one in the ''last'' index. "Column major order" is analogous with respect to the ''first'' index. In systems which use [[processor cache]] or [[virtual memory]], scanning an array is much faster if successive elements are stored in consecutive positions in memory, rather than sparsely scattered. This is known as spatial locality, which is a type of [[locality of reference]]. Many algorithms that use multidimensional arrays will scan them in a predictable order. A programmer (or a sophisticated compiler) may use this information to choose between row- or column-major layout for each array. For example, when computing the product ''A''Β·''B'' of two matrices, it would be best to have ''A'' stored in row-major order, and ''B'' in column-major order.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)