Sign in Join
Wiki copytemporary

Array: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-An **array** is a linear data structure concept that groups elements of the same type, stores them in contiguous and adjacent memory locations and provides random access to all of said elements (also known as components) by way of a linear index.
1+An **array** is a linear data structure concept that groups elements of the same type, stores them in contiguous and adjacent memory locations and provides random access to all of said elements (also known as components) by way of a linear index.
22
3-The word `array` is a [[Reserved word|reserved word]]. It always occurs in conjunction with the word [[Of|of]].
3+The word `array` is a [[Reserved word|reserved word]]. It always occurs in conjunction with the word [[Of|`of`]].
44
55## Notion
66
7-An `array` is a limited and arranged aggregation of elements, all having the same [[Data type|data type]] which is called the “base type.” It has at least one discrete, bounded dimension and continuously enumerates all of its elements. Each element can be uniquely identified by one or more scalar values, called *indices*, along those dimensions.
7+An `array` is a limited and arranged aggregation of elements, all having the same [[Data type|data type]] which is called the “base type.” It has at least one discrete, bounded dimension and continuously enumerates all of its elements. Each element can be uniquely identified by one or more scalar values, called _indices_ , along those dimensions.
88
9-A one-dimensional `array` resembles an **n-tuple**, as it is known in mathematics, but has the constraint of being homogenous (all elements must be of the same type). The range of all possible values such an `array` can acquire is the homogenous n-ary Cartesian product of the base type.
9+A one-dimensional `array` resembles an **n-tuple** , as it is known in mathematics, but has the constraint of being homogenous (all elements must be of the same type). The range of all possible values such an `array` can acquire is the homogenous n-ary Cartesian product of the base type.
1010
11-A two-dimensional `array` resembles the mathematical concept of a **matrix**, except for the homogeneity restriction.
11+A two-dimensional `array` resembles the mathematical concept of a **matrix** , except for the homogeneity restriction.
1212
1313## Usage
1414
1515### Length
1616
17-Originally, Pascal only provided arrays of fixed length ([[Standard Pascal]]), meaning the number of elements an array consisted of had to be known at compile-time. Since this turned out to be a major constraint, and changes in computer hardware since then justified a step forward, variable-length arrays were introduced.
18-
19-[[Extended Pascal]] defined the notion of “schemata” for this. [[Delphi]] introduced “dynamic arrays”. As of 2020, [[FPC]] only supports the latter regarding variable-length arrays, while support for “schemata” is planned.
17+Originally, Pascal only provided arrays of fixed length ([[Standard Pascal]]), meaning the number of elements an array consisted of had to be known at compile-time. Since this turned out to be a major constraint, and changes in computer hardware since then justified a step forward, variable-length arrays were introduced.
2018
21-Depending on whether an array is intended of being capable of changing its size, its definition varies, but just marginally. For a one-dimensional static array, the type definition looks like this:
19+[[Extended Pascal]] defined the notion of “schemata” for this. [[Delphi]] introduced “dynamic arrays”. As of 2020, [[FPC]] only supports the latter regarding variable-length arrays, while support for “schemata” is planned.
2220
23-``` pascal
21+Depending on whether an array is intended of being capable of changing its size, its definition varies, but just marginally. For a one-dimensional static array, the type definition looks like this:
22+```
2423array[indexType] of baseType
2524```
2625
27-A dynamic array type definition is simply relieved of its dimension specification:
28-
29-``` pascal
26+A dynamic array type definition is simply relieved of its dimension specification:
27+```
3028array of baseType
3129```
3230
3331### Static arrays
3432
35-In static arrays, the ranges of all dimensions are known in advance. All dimension specifications have to be ordinal types. The following code shows some valid array definitions, all of them static.
36-
37-``` pascal
33+In static arrays, the ranges of all dimensions are known in advance. All dimension specifications have to be ordinal types. The following code shows some valid array definitions, all of them static.
34+```
3835program staticArrayDemo(input, output, stderr);
39-
40-type
41- // specifying ordinal types as index directly
4236
43- /// allows selection of a character
44- /// based on a Boolean value
45- characterChoice = array[boolean] of UCS4char;
46-
47- // enumerations
48-
49- /// enumerates Cartesian axes
50- spaceAxis = (xAxis, yAxis, zAxis);
51- /// a point in three-dimensional Euclidean space
52- locus = array[spaceAxis] of valReal;
53- /// a point in a two-dimensional Euclidean plane
54- point = array[xAxis..yAxis] of valReal;
55-
56- // integer subranges
57-
58- level = array[-24..24] of longint;
59- box = array[-1..1, -1..1, -1..1] of boolean;
60- transformationMatrix = array[0..1, 0..1] of valReal;
61-begin
62-end.
37+ type
38+ // specifying ordinal types as index directly
39+
40+ /// allows selection of a character
41+ /// based on a Boolean value
42+ characterChoice = array[boolean] of UCS4char;
43+
44+ // enumerations
45+
46+ /// enumerates Cartesian axes
47+ spaceAxis = (xAxis, yAxis, zAxis);
48+ /// a point in three-dimensional Euclidean space
49+ locus = array[spaceAxis] of valReal;
50+ /// a point in a two-dimensional Euclidean plane
51+ point = array[xAxis..yAxis] of valReal;
52+
53+ // integer subranges
54+
55+ level = array[-24..24] of longint;
56+ box = array[-1..1, -1..1, -1..1] of boolean;
57+ transformationMatrix = array[0..1, 0..1] of valReal;
58+ begin
59+ end.
6360```
6461
65-As all of an array’s elements have to be addressable, there exists a maximum limit of elements an array can hold. The [[SizeOf|sizeOf]] every array type has to be less than [`ptrInt`](https://www.freepascal.org/docs-html/rtl/system/ptrint.html)’s maximum value.
62+As all of an array’s elements have to be addressable, there exists a maximum limit of elements an array can hold. The [[SizeOf|`sizeOf`]] every array type has to be less than [`ptrInt`](<https://www.freepascal.org/docs-html/rtl/system/ptrint.html>)’s maximum value.
6663
6764### Initialization
6865
6966It's possible to set the initial values of a static (and dynamic) array's elements when it is declared –
70-
71-``` pascal
67+```
7268var
73- SArray : array[0..2] of integer = (1,2,3); // A static Array
74- CArray : array[0..1] of TColor = (clRed, clBlue); // A static Array
69+ SArray : array[0..2] of integer = (1,2,3); // A static Array
70+ CArray : array[0..1] of TColor = (clRed, clBlue); // A static Array
7571```
7672
77-  **See more:** [Topic: How to initialize the array (Free Pascal Lazarus Forum)](https://forum.lazarus.freepascal.org/index.php/topic,52656.15.html)
73+**See more:** [Topic: How to initialize the array (Free Pascal Lazarus Forum)](<https://forum.lazarus.freepascal.org/index.php/topic,52656.0.html>)
7874
7975### Addressing elements
8076
81-An array’s element is addressed by naming the array variable’s identifier, followed by a valid index value enclosed by square brackets.
82-
83-``` pascal
77+An array’s element is addressed by naming the array variable’s identifier, followed by a valid index value enclosed by square brackets.
78+```
8479program arrayAddressDemo(input, output, stderr);
85-var
86- msg: array[0..2] of char;
87-begin
88- msg[0] := 'H';
89- msg[1] := 'i';
90- msg[2] := '!';
91- writeLn(msg);
92-end.
80+ var
81+ msg: array[0..2] of char;
82+ begin
83+ msg[0] := 'H';
84+ msg[1] := 'i';
85+ msg[2] := '!';
86+ writeLn(msg);
87+ end.
9388```
9489
95-Multidimensional arrays’ elements can be addressed in two ways: either by [[Comma|comma]]-separated indices…
96-
97-``` pascal
90+Multidimensional arrays’ elements can be addressed in two ways: either by [[Comma|comma]]-separated indices…
91+```
9892arrayVariable[firstDimensionIndex, secondDimensionIndex, thirdDimensionIndex]
9993```
10094
101-…or by putting indices in dedicated square brackets.
102-
103-``` pascal
95+…or by putting indices in dedicated square brackets.
96+```
10497arrayVariable[firstDimensionIndex][secondDimensionIndex][thirdDimensionIndex]
10598```
10699
107-A third syntactically-valid option would be mixing both styles, however that is considered poor style, unless perhaps there is indication to group indices (e.g. `x`, `y` and `z` coordinates versus other indices) it is okay. Nonetheless, only the first mentioned notation is valid while defining array types.
108-
109-Note, it is very important to specify indices in the defined order, within each dimensions’ range. Consider the following program; it will compile, but fail during [[runtime|run-time]] due to `{$rangeChecks on}`:
100+A third syntactically-valid option would be mixing both styles, however that is considered poor style, unless perhaps there is indication to group indices (e.g. `x`, `y` and `z` coordinates versus other indices) it is okay. Nonetheless, only the first mentioned notation is valid while defining array types.
110101
111-``` pascal
102+Note, it is very important to specify indices in the defined order, within each dimensions’ range. Consider the following program; it will compile, but fail during [[runtime|run-time]] due to `{$rangeChecks on}`:
103+```
112104program arrayAddressOrderDemo(input, output, stderr);
113-{$rangeChecks on}
114-var
115- i: integer;
116- f: array[0..1, 0..3] of boolean;
117-begin
118- for i := 0 to 7 do
105+ {$rangeChecks on}
106+ var
107+ i: integer;
108+ f: array[0..1, 0..3] of boolean;
119109 begin
120- f[0, i] := true;
121- end;
122-end.
110+ for i := 0 to 7 do
111+ begin
112+ f[0, i] := true;
113+ end;
114+ end.
123115```
124116
125-While the program would indeed iterate over every array’s elements, it doesn’t do so in the intended way, but rather exploits the fact that the array’s internal memory structure is just a continuous block of memory. This is bad style. A programmer working with a high-level language is not supposed to care about specific memory layouts. Cave: It is possible to tamper with other variables in this way. At any rate, a [[runtime error|run-time error]], namely “RTE 216 general protection fault,” will occur if an attempt is made to access memory which is not within the purview of the process owner.
117+While the program would indeed iterate over every array’s elements, it doesn’t do so in the intended way, but rather exploits the fact that the array’s internal memory structure is just a continuous block of memory. This is bad style. A programmer working with a high-level language is not supposed to care about specific memory layouts. Cave: It is possible to tamper with other variables in this way. At any rate, a [[runtime error|run-time error]], namely “RTE 216 general protection fault,” will occur if an attempt is made to access memory which is not within the purview of the process owner.
126118
127-When values contained in arrays are merely read (and thus the indices do not matter), a [[for-in loop|for … in loop]] can be used.
119+When values contained in arrays are merely read (and thus the indices do not matter), a [[for-in loop|`for … in` loop]] can be used.
128120
129121## Dynamic arrays
130122
131-A [[Dynamic array|dynamic array]] is an approach for overcoming the limitation of knowing the sizes of all dimensions in advance. See its dedicated page for more details.
123+A [[Dynamic array|dynamic array]] is an approach for overcoming the limitation of knowing the sizes of all dimensions in advance. See its dedicated page for more details.
132124
133125## Application
134126
135-See, for instance:
127+See, for instance:
136128
137-- [[Array sort]]
138-- [[15-puzzle]]’s and [[Peg Solitaire tutorial|Peg]]’s game board states are stored as an array.
129+ * [[Array sort]]
130+ * [[15-puzzle]]’s and [[Peg Solitaire tutorial|Peg]]’s game board states are stored as an array.
139131
140-In the default [[RTL]]’s system unit, the function [`system.slice`](https://www.freepascal.org/docs-html/rtl/system/slice.html) returns the initial part of an array, similar to the Ruby notation `arrayVariable[0, n]`. Furthermore, there is [`system.arrayStringToPPchar`](https://www.freepascal.org/docs-html/rtl/system/arraystringtoppchar.html). Most [[Functions for descriptive statistics|statistical routines of the RTL’s math unit]] accept arrays as parameters, as well as some other routines.
132+In the default [[RTL]]’s system unit, the function [`system.slice`](<https://www.freepascal.org/docs-html/rtl/system/slice.html>) returns the initial part of an array, similar to the Ruby notation `arrayVariable[0, n]`. Furthermore, there is [`system.arrayStringToPPchar`](<https://www.freepascal.org/docs-html/rtl/system/arraystringtoppchar.html>). Most [[Functions for descriptive statistics|statistical routines of the RTL’s math unit]] accept arrays as parameters, as well as some other routines.
141133
142134## See also
143135
144-- [[Type information]]
145-- [[Defensive programming techniques]]
146-- [[Vectorization]]
147-- [[Flexible Array Member]]
148-- Tutorial: [[Basic Pascal Tutorial/Chapter 5/1-dimensional arrays|1-dimensional arrays]]
149-- Tutorial: [[Basic Pascal Tutorial/Chapter 5/Multidimensional arrays|Multidimensional arrays]]
150-- Example: [[Example: Multidimensional dynamic array|Multidimensional dynamic array]]
151-- Article: [[Why Pascal is Not My Favorite Programming Language|Why Pascal is not my favorite programming language § “The size of an array is part of its type”]]
152-- [`matrix` unit](https://www.freepascal.org/docs-html/rtl/matrix/index.html)
136+ * [[Type information]]
137+ * [[Defensive programming techniques]]
138+ * [[Vectorization]]
139+ * [[Flexible Array Member]]
140+ * Tutorial: [[Basic Pascal Tutorial/Chapter 5/1-dimensional arrays|1-dimensional arrays]]
141+ * Tutorial: [[Basic Pascal Tutorial/Chapter 5/Multidimensional arrays|Multidimensional arrays]]
142+ * Example: [[Example: Multidimensional dynamic array|Multidimensional dynamic array]]
143+ * Article: [[Why Pascal is Not My Favorite Programming Language|Why Pascal is not my favorite programming language § “The size of an array is part of its type”]]
144+ * [`matrix` unit](<https://www.freepascal.org/docs-html/rtl/matrix/index.html>)
153145
146+
147+
148+navigation bar: data types [[simple type|simple data types]] | [[Boolean|`boolean`]] [[Byte|`byte`]] [[Cardinal|`cardinal`]] [[Char|`char`]] [[Currency|`currency`]] [[Double|`double`]] [`dword`](</index.php?title=DWord&action=edit&redlink=1> "DWord \(page does not exist\)") [[Extended|`extended`]] [[Int8|`int8`]] [[Int16|`int16`]] [[Int32|`int32`]] [[Int64|`int64`]] [[Integer|`integer`]] [[Longint|`longint`]] [[Real|`real`]] [[Shortint|`shortint`]] [[Single|`single`]] [[Smallint|`smallint`]] [[Pointer|`pointer`]] [[QWord|`qword`]] [[Word|`word`]]
149+---|---
150+complex data types | `array` [[Class|`class`]] [[Object|`object`]] [[Record|`record`]] [[Set|`set`]] [[String|`string`]] [[Shortstring|`shortstring`]]
151+
152+
153+
154+****
155+
154156---
155-*From the Free Pascal wiki, page "Array", as it stood on 2023-09-20 after a change by RogueScholar, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
157+*From the Free Pascal wiki, page "Array", as the Internet Archive kept it on 2026-01-21, last changed on 2024-11-03. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history