Difference between revisions of "Dynamic array/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 37: Line 37:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
動的配列のインデックスは、0「ゼロ」が基底となります。すなわち、範囲は 0 から (Length-1)までとなります。これを 1「イチ」 を基底にすることはできません。
+
動的配列のインデックスは、0「ゼロ」が下限となります。すなわち、範囲は 0 から (Length-1)までとなります。 これを、1「イチ」 がインデックスの下限になるようには、できません。
  
 
実際には、動的配列は自動的に再参照(automatic dereferencing)を行うポインタです。 それらは、自動的に '''nil''' に初期化されます。 これは、 変数 '''MyVariable''' を '''fillchar''', '''sizeof''',などの低レベルのルーチンを通して扱うとき、ポインタ変数として翻訳されることを意味します。 しかし、その要素をインデックス化する際( in '''MyVariable[2]''' のように)、または、その配列を入出力とするルーチンに渡されるとき、それは自動的に '''MyVariable^'''  に展開されます。
 
実際には、動的配列は自動的に再参照(automatic dereferencing)を行うポインタです。 それらは、自動的に '''nil''' に初期化されます。 これは、 変数 '''MyVariable''' を '''fillchar''', '''sizeof''',などの低レベルのルーチンを通して扱うとき、ポインタ変数として翻訳されることを意味します。 しかし、その要素をインデックス化する際( in '''MyVariable[2]''' のように)、または、その配列を入出力とするルーチンに渡されるとき、それは自動的に '''MyVariable^'''  に展開されます。
Line 43: Line 43:
 
メモリー管理の点からは、動的配列は単純なポインタです。 '''SetLength''' がアロケートを行い、また、必要に応じてヒープ上のメモリを解放します。 ファンクションやプロシージャとして扱うときは、ポインタのみがスタックに加えられます。 プロシージャから出るときは、動的配列の値は取り除かれ、メモリーはまた利用できるようになります。 実際には、メモリー管理プロシージャは実行プログラムに挿入されており、その結果として、全体的にプログラマーにとって見通しが良くなっています。  
 
メモリー管理の点からは、動的配列は単純なポインタです。 '''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)'''.
+
動的配列に '''nil''' をアサインすることは、自動的にポインタが示す位置のメモリーを開放します。 それは、 '''SetLength(MyVariable, 0)''' と一致します。 これには、副次的な効果もあり、もしポインタ値がなんらかの理由(i.e., 以前のプログラムが実行したときに保存された位置に、ディスクから読み込まれたなど) でif the pointer value is not valid for some reason 。 このような invalid なポインタを初期化するためには、 '''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.
 
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.

Revision as of 12:56, 7 May 2017

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,などの低レベルのルーチンを通して扱うとき、ポインタ変数として翻訳されることを意味します。 しかし、その要素をインデックス化する際( in MyVariable[2] のように)、または、その配列を入出力とするルーチンに渡されるとき、それは自動的に MyVariable^ に展開されます。

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

動的配列に nil をアサインすることは、自動的にポインタが示す位置のメモリーを開放します。 それは、 SetLength(MyVariable, 0) と一致します。 これには、副次的な効果もあり、もしポインタ値がなんらかの理由(i.e., 以前のプログラムが実行したときに保存された位置に、ディスクから読み込まれたなど) でif the pointer value is not valid for some reason 。 このような invalid なポインタを初期化するためには、 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.

関連情報