Difference between revisions of "Dynamic array/ru"

From Free Pascal wiki
Jump to navigationJump to search
Line 13: Line 13:
 
Невозможно использовать перечислительный тип, любой другой порядковый тип в качестве индекса или изменить первый элемент, заданный индексом <syntaxhighlight lang="pascal" enclose="none">1</syntaxhighlight>.
 
Невозможно использовать перечислительный тип, любой другой порядковый тип в качестве индекса или изменить первый элемент, заданный индексом <syntaxhighlight lang="pascal" enclose="none">1</syntaxhighlight>.
  
=== definition ===
+
=== Определение ===
A one-dimensional dynamic array is defined like this:
+
Одномерный динамический массив определяется так:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
array of char
 
array of char
 
</syntaxhighlight>
 
</syntaxhighlight>
Note, how no dimensions' size is specified.
+
Обратите внимание, что размерность [массива] не указан.
  
In order to define a multidimensional array, an array itself is specified as the base type.
+
Для определения многомерного массива в качестве базового типа указывается сам массив.
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
array of array of longInt
 
array of array of longInt

Revision as of 01:34, 9 May 2019

English (en) español (es) suomi (fi) français (fr) 日本語 (ja) русский (ru)

Динамический массив - это массив, размеры которого неизвестны во время компиляции. Динамический массив не является единственным типом, предоставляющим массивы переменной длины, но с 2018 года он является единственным, который поддерживает FPC.

Использование

Концепция

Определение динамического массива будет выделять пространство только для указателя. Во время выполнения различные подпрограммы будут обеспечивать удобное использование, но что более важно, синтаксис доступа к элементам массива путем помещения индексов в квадратные скобки поддерживается компилятором (реализован как автоматическое разыменование указателя).

Индексы динамических массивов всегда являются неотрицательными целыми числами, начинающимися с нуля для первого элемента. Невозможно использовать перечислительный тип, любой другой порядковый тип в качестве индекса или изменить первый элемент, заданный индексом 1.

Определение

Одномерный динамический массив определяется так:

array of char

Обратите внимание, что размерность [массива] не указан.

Для определения многомерного массива в качестве базового типа указывается сам массив.

array of array of longInt

sizing

The compiler procedure setLength will change a dynamic array's length, provided there is enough memory.

1program setLengthDemo(input, output, stdErr);
2var
3	sieve: array of longWord;
4begin
5	setLength(sieve, 1337);
6end.

The procedure allocates memory for as many records of the base type as specified, plus some management data. It then copies all elements of the old incarnation to the new one. New fields, that did not exist before, are initialized with the default intrinsic.

Multidimensional arrays can be resized with setLength, too.

1program multidimensionalSetLengthDemo(input, output, stdErr);
2var
3	samples: array of array of smallInt;
4begin
5	setLength(samples, 12, 64);
6end.

Valid indices for samples' first dimension are in 0..11, while valid indices for its second dimension are within the range 0..63.

One quite useful fact is, the limitation all dimensions have to be of the same size does not apply to dynamic arrays.

 1program binomialPotence(input, output, stdErr);
 2var
 3	pascalsTriangle: array of array of longWord;
 4	exponent: longInt;
 5	factor: longInt;
 6begin
 7	setLength(pascalsTriangle, 20);
 8	
 9	setLength(pascalsTriangle[0], 1);
10	pascalsTriangle[0][0] := 1;
11	
12	setLength(pascalsTriangle[1], 2);
13	pascalsTriangle[1][0] := 1;
14	pascalsTriangle[1][1] := 1;
15	
16	// construct values by simple addition
17	for exponent := 2 to high(pascalsTriangle) do
18	begin
19		setLength(pascalsTriangle[exponent], exponent + 1);
20		pascalsTriangle[exponent][0] := 1;
21		pascalsTriangle[exponent][exponent] := 1;
22		for factor := 1 to exponent - 1 do
23		begin
24			pascalsTriangle[exponent][factor] :=
25				pascalsTriangle[exponent - 1][factor - 1] +
26				pascalsTriangle[exponent - 1][factor];
27		end;
28	end;
29	
30	// ...

initializing

Since FPC 3.0.0 dynamic array types that are not anonymous are automatically equipped with a “constructor” as it might be familiar from object-oriented programming. This lets you unite setLength calls and a series of assignments in one statement:

1program dynamicArrayCreateDemo(input, output, stdErr);
2type
3	truths = array of boolean;
4var
5	l: truths;
6begin
7	l := truths.create(false, true, true, false, true, false, false);
8end.

Of course you can nest arrays as well:

 1program nestedDynamicArrayCreateDemo(input, output, stdErr);
 2type
 3	truths = array of boolean;
 4	pattern = array of truths;
 5var
 6	p: pattern;
 7begin
 8	p := pattern.create(
 9			truths.create(false, false),
10			truths.create(true, false),
11			truths.create(true, false, false),
12			truths.create(true, true, false)
13		);
14end.

handling

Keep in mind dynamic arrays are pointers. Assigning dynamic array variables to each other does not copy any payload, but just the address. This differs from static arrays' behavior.

If you want to duplicate data you have to use system.copy.

 1program dynamicArrayCopyDemo(input, output, stdErr);
 2
 3var
 4	foo, bar: array of char;
 5
 6procedure printArrays;
 7begin
 8	writeLn('foo[0] = ', foo[0], '; bar[0] = ', bar[0]);
 9end;
10
11begin
12	setLength(foo, 1);
13	foo[0] := 'X';
14	// copies _reference_
15	bar := foo;
16	write('     initial values: ');
17	printArrays;
18	
19	// change content _via_ _second_ reference
20	bar[0] := 'O';
21	write('changed via 2nd ref: ');
22	printArrays;
23	
24	// copy content
25	bar := copy(foo, 0, length(foo));
26	bar[0] := 'X';
27	write(' copied and changed: ');
28	printArrays;
29end.

Only by using copy both arrays can be modified independently.

As stated above, setLength copies data. The highlighted line in the example above is (semantically) equivalent to setLength(bar, length(bar)).

Dynamic arrays are reference counted. Calling setLength(myDynamicArrayVariable, 0) virtually does myDynamicArrayVariable := nil and decreases the reference count. Only when the reference count hits zero, the memory block is released.

 1program dynamicArrayNilDemo(input, output, stdErr);
 2var
 3	foo, bar: array of char;
 4begin
 5	setLength(foo, 1);
 6	foo[0] := 'X';
 7	// copy _reference_, increase reference count
 8	bar := foo;
 9	// foo becomes nil, reference count is decreased
10	setLength(foo, 0);
11	writeLn('length(foo) = ', length(foo),
12		'; length(bar) = ', length(bar));
13	
14	// decrease reference count another time
15	bar := nil;
16	writeLn('length(foo) = ', length(foo),
17		'; length(bar) = ', length(bar));
18end.

Nonetheless, dynamic arrays are finalized automatically. It is not necessary to manually setLength(, 0) on all your references when the program comes to end, or when leaving a scope in general.

Without {$rangeChecks on} it is possible to reach beyond an array's limits. That means when iterating over dynamic arrays, it is impossible to work without low and high to determine valid indices (the former being optional, since dynamic arrays always start at zero). Alternatively, for in loops can be used, if no index is required.

Remember, sizeOf of a dynamic array evaluates to the size of a pointer.

application

see also