Dynamic array/ja

From Lazarus wiki
Jump to navigationJump to search

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

動的配列は、 FreePascal ( と Delphi) のとても便利な機能です。それは、 配列 型ととてもよく似ていますが、プログラムの実行時以前に要素数を決める必要がないことから、プログラマーにより高い柔軟性をもたらします。

宣言部は、配列 型においても大変シンプルな記述となっています。:

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

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

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

SetLength 宣言は、配列を拡大したり切り詰めたりするために、プログラム中で何回でも、どの位置でも宣言できますが、初めて配列を用いる前に、少なくとも1回は宣言する必要があります。

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

...
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「イチ」 を基底にすることはできません。

実際には、動的配列は自動的に再参照(automatic dereferencing)を行うポインタです。 それらは、自動的に nil に初期化されます。 これは、 変数 MyVariablefillchar, sizeof,などの低レベルのルーチンを通して扱うとき、ポインタ変数として翻訳されることを意味します。 しかし、その要素または期待された配列の種類を超えた参照をされたとき、それは自動的に拡張されます。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.

メモリー管理の点からは、動的配列は単純なポインタです。 SetLength がアロケートを行い、また、必要に応じてヒープ上のメモリを解放します。 ファンクションやプロシージャとして扱うときは、ポインタのみがスタックに加えられます。 プロシージャから出るときは、動的配列の値は取り除かれ、メモリーはまた利用できるようになります。 実際には、メモリー管理プロシージャは実行プログラムに挿入されており、その結果として、全体的にプログラマーにとって見通しが良くなっています。

動的配列に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.

関連情報