[fpc-pascal] Dynamic arrays, yet another pitfall

Michael Van Canneyt michael at freepascal.org
Sat Feb 15 19:41:05 CET 2014



On Sat, 15 Feb 2014, Jürgen Hestermann wrote:

> Am 2014-02-10 09:13, schrieb Michael Van Canneyt:
>>> No. There is no COW, only ref. counting. SetLength just forces an unique
>>> instance of the array if needed.
>> The documentation explicitly mentions that:
>> 'Dynamic arrays are reference counted: assignment of one dynamic array-type
>> variable to another will let both variables point to the same array.
>> Contrary to ansistrings, an assignment to an element of one array will
>> be reflected in the other: there is no copy-on-write.'
>
> But what do these sentences say?
> They say that there is *no* copy-on-write.

Correct.

> So why does it suddenly do copy-on-write when using SetLength?

Copy-On-Write means that if you write to *an element* of the array, 
the array is duplicated if it has a ref count>0. As with strings.

Since this does not happen for dynamic arrays, there is no copy on write.

That setlength behaves rather freakish for dynamic arrays, does not 
mean we have copy-on-write.

Michael.


More information about the fpc-pascal mailing list