<p>Am 13.04.2017 13:25 schrieb "MARCOU Gilles" <<a href="mailto:g.marcou@unistra.fr">g.marcou@unistra.fr</a>>:<br>
><br>
> Regarding this code:<br>
><br>
>> SetLength(Array,Length(Array)+1);<br>
>> Array[High(Array)] := …<br>
><br>
><br>
> as I understood from (<a href="http://wiki.freepascal.org/Dynamic_array">http://wiki.freepascal.org/Dynamic_array</a>), SetLength will create a copy of the array and free the memory of the shorter array. In this case, a lot of memory operations and copy operations are performed thus degrading the performances of the code. Then it would be unwise to use such strategy: better allocate the size of the array correctly at the beginning of the run, or resize the array by block, to decrease the frequency of such operation:<br>
><br>
>> SetLength(Array,Length(Array)+N);<br>
><br>
><br>
> Can somebody confirm (or invalidate) this?</p>
<p>SetLength() uses reallocation for the memory. So it *might* work without copying though that is not guaranteed.<br>
But in general it's recommended anyway not to do "SetLength(..., ... + 1)" loops as that will lead to fragmentation of the heap.</p>
<p>Regards<br>
Sven</p>