Difference between revisions of "Dynamic array/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{Dynamic array}}
 
{{Dynamic array}}
  
The dynamic array type is a very nice feature of FreePascal (and Delphi) syntax. It is very similar to the [[Array|array]] type, but allows more flexibility for the programmer since the number of elements does not need to be known until program execution.
+
動的配列は、 FreePascal ( Delphi) のとても便利な機能です。それは、[[Array/ja|配列]型ととてもよく似ていますが、プログラムの実行プログラマーにより高い柔軟性を許します。since the number of elements does not need to be known until program execution.
  
The declaration part is just as simple as for the [[Array|array]] type:   
+
宣言部は、The declaration part is just as simple as for the [[Array|array]] type:   
 
<syntaxhighlight>
 
<syntaxhighlight>
 
var
 
var
Line 11: Line 11:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The number of elements can be set or modified whenever needed during the execution of the program by inserting the statement:
+
要素の数は、 SetLength 宣言を挿入することで、プログラムの実行中に、いつでも必要に応じて設定することができます。:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 22: Line 22:
 
You can put as many '''SetLength''' statements as you want in your program in order to expand or truncate an array but you must put at least one statement before you can use the variable for the first time.  
 
You can put as many '''SetLength''' statements as you want in your program in order to expand or truncate an array but you must put at least one statement before you can use the variable for the first time.  
  
The individual elements can be accessed as follows:
+
個々の要素へのアクセスは、以下のようにして行います。:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
...
 
...
Line 37: Line 37:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The index of a dynamic array is ZERO based, i.e. it must be within the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.
+
動的配列のインデックスは、0「ゼロ」が基底となります。すなわち、範囲は 0 から (Length-1)までとなります。これを 1「イチ」 を基底にすることはできません。
  
Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to '''nil''' automatically. This means, that '''MyVariable''' is interpreted as a pointer variable when handed over to low level routines like '''fillchar''', '''sizeof''', etc. but it is automatically expanded to '''MyVariable^''' when indexing its elements (as in '''MyVariable[2]''') or when handing it over to routines that expect array types.
+
実際には、Actually, 動的配列はポインタへの自動的な参照のポインタです。dynamic arrays are pointers with automatic dereferencing. それらは、自動的に '''nil''' に初期化されます。They are initialized to '''nil''' automatically. これは、 '''fillchar''', '''sizeof''',などの低レベルのルーチンを通して扱われるとき、 '''MyVariable''' がポインタ変数として翻訳されることを意味します。 This means, that '''MyVariable''' is interpreted as a pointer variable when handed over to low level routines like '''fillchar''', '''sizeof''', etc. しかし、but it is automatically expanded to '''MyVariable^''' when indexing its elements (as in '''MyVariable[2]''') or when handing it over to routines that expect array types.
  
 
From a memory management view, dynamic array variables are simple pointers. '''SetLength''' allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the memory is made available again. In fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.   
 
From a memory management view, dynamic array variables are simple pointers. '''SetLength''' allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the memory is made available again. In fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.   
Line 47: Line 47:
 
Although writing to elements of dynamic arrays does '''not''' create a new instance of the array (no copy-on-write as it exists for Ansistrings) using '''SetLength''' on such arrays '''does''' create a copy! So if 2 dynamic array variables point to the same array (one has been assigned to the other) they do not do so after using '''SetLength''' on one (or both) of them. After the SetLength() call the two variables are distinct arrays whose elements are independent from each other.
 
Although writing to elements of dynamic arrays does '''not''' create a new instance of the array (no copy-on-write as it exists for Ansistrings) using '''SetLength''' on such arrays '''does''' create a copy! So if 2 dynamic array variables point to the same array (one has been assigned to the other) they do not do so after using '''SetLength''' on one (or both) of them. After the SetLength() call the two variables are distinct arrays whose elements are independent from each other.
  
== 参考 ==
+
== 関連情報 ==
 
* [http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1 Free Pascal Reference guide: Dynamic arrays]
 
* [http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1 Free Pascal Reference guide: Dynamic arrays]
 
* [[Example: Multidimensional dynamic array/ja]]
 
* [[Example: Multidimensional dynamic array/ja]]
 
* [[Array/ja]]
 
* [[Array/ja]]

Revision as of 14:31, 4 May 2017

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

動的配列は、 FreePascal ( と Delphi) のとても便利な機能です。それは、[[Array/ja|配列]型ととてもよく似ていますが、プログラムの実行プログラマーにより高い柔軟性を許します。since the number of elements does not need to be known until program execution.

宣言部は、The declaration part is just as simple as for the array type:

var
  ...
  MyVariable : array of type;
  ...

要素の数は、 SetLength 宣言を挿入することで、プログラムの実行中に、いつでも必要に応じて設定することができます。:

begin
  ...
  SetLength(MyVariable, ItsNewLength);
  ...
end

You can put as many SetLength statements as you want in your program in order to expand or truncate an array but you must put at least one statement before you can use the variable for the first time.

個々の要素へのアクセスは、以下のようにして行います。:

...
SetLength(MyVariable,19);
...
MyVariable[18] := 123;
...
MyOtherVariable := MyVariable[0];
...
WriteLn('MyVariable has ', Length(MyVariable), ' elements');  {should be 19}
...
WriteLn('Its range is ', Low(MyVariable), ' to ', High(MyVariable)); {should be 0 to 18}
...

動的配列のインデックスは、0「ゼロ」が基底となります。すなわち、範囲は 0 から (Length-1)までとなります。これを 1「イチ」 を基底にすることはできません。

実際には、Actually, 動的配列はポインタへの自動的な参照のポインタです。dynamic arrays are pointers with automatic dereferencing. それらは、自動的に nil に初期化されます。They are initialized to nil automatically. これは、 fillchar, sizeof,などの低レベルのルーチンを通して扱われるとき、 MyVariable がポインタ変数として翻訳されることを意味します。 This means, that MyVariable is interpreted as a pointer variable when handed over to low level routines like fillchar, sizeof, etc. しかし、but it is automatically expanded to MyVariable^ when indexing its elements (as in MyVariable[2]) or when handing it over to routines that expect array types.

From a memory management view, dynamic array variables are simple pointers. SetLength allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the memory is made available again. In fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.

Assigning nil to a dynamic array variable automatically frees the memory where the pointer pointed to. It's identical to SetLength(MyVariable, 0). This can have a side effect, if the pointer value is not valid for some reason (i.e., if it was read from disk where it was stored from previous program runs). To init such an invalid pointer you have to use FillChar(MyVariable,sizeof(MyVariable), #0).

Although writing to elements of dynamic arrays does not create a new instance of the array (no copy-on-write as it exists for Ansistrings) using SetLength on such arrays does create a copy! So if 2 dynamic array variables point to the same array (one has been assigned to the other) they do not do so after using SetLength on one (or both) of them. After the SetLength() call the two variables are distinct arrays whose elements are independent from each other.

関連情報