Content deleted Content added
m clean up, typo(s) fixed: purpose built → purpose-built |
m →Applications: Minor wording changes |
||
(12 intermediate revisions by 7 users not shown) | |||
Line 4:
{{More citations needed|date=September 2008}}
In [[computer science]], an '''array''' is a [[data structure]] consisting of a collection of ''elements'' ([[value (computer science)|values]] or [[variable (programming)|variables]]), of same memory size, each identified by at least one ''array index'' or ''key'', a collection of which may be a [[tuple]], known as an index tuple. An array is stored such that the position (memory address) of each element can be computed from its index
For example, an array of ten [[32-bit]] (4-byte) integer variables, with indices 0 through 9, may be stored as ten [[Word (data type)|words]] at memory addresses 2000, 2004, 2008, ..., 2036, (in [[hexadecimal]]: <code>0x7D0</code>, <code>0x7D4</code>, <code>0x7D8</code>, ..., <code>0x7F4</code>) so that the element with index ''i'' has the address 2000 + (''i'' × 4).<ref>David R. Richardson (2002), The Book on Data Structures. iUniverse,
The memory address of the first element of an array is called first address, foundation address, or base address.
Line 31:
One or more large arrays are sometimes used to emulate in-program [[dynamic memory allocation]], particularly [[memory pool]] allocation. Historically, this has sometimes been the only way to allocate "dynamic memory" portably.
Arrays can be used to determine partial or complete [[control flow]] in programs, as a compact alternative to (otherwise repetitive) multiple <code>IF</code> statements.
==Element identifier and addressing formulas==
Line 60:
===One-dimensional arrays===
[[File:1D array diagram.svg|thumb|Diagram of a typical 1D array]]
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index.
Line 69 ⟶ 70:
However, one can choose the index of the first element by an appropriate choice of the base address ''B''. For example, if the array has five elements, indexed 1 through 5, and the base address ''B'' is replaced by {{nowrap|''B'' + 30''c''}}, then the indices of those same elements will be 31 to 35. If the numbering does not start at 0, the constant ''B'' may not be the address of any element.
[[File:2D array diagram.svg|thumb|Diagram of a typical 2D array]]
===Multidimensional arrays===
[[File:3D array diagram.svg|thumb|Diagram of a typical 3D array]]
For a multidimensional array, the element with indices ''i'',''j'' would have address ''B'' + ''c'' · ''i'' + ''d'' · ''j'', where the coefficients ''c'' and ''d'' are the ''row'' and ''column address increments'', respectively.
|