[fpc-pascal] Dynamic arrays, yet another pitfall
Jürgen Hestermann
juergen.hestermann at gmx.de
Sat Feb 15 19:26:15 CET 2014
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.
So why does it suddenly do copy-on-write when using SetLength?
SetLength is a write to the array. It adds (or removes) elements.
So why do I get a copy? The documentation says that I will *not* get a copy (no COW).
So the documentation is wrong (or at least totaly misleading/incomplete).
> But I have added a paragraph about the "reset ref. count" behaviour of setlength.
What has a reference counter to do with the behaviour I see?
If SetLength would just simply change the number of elements it would not change a reference counter.
So nothing else happens, just elements are added or removed.
The same as when I change an element.
At least that's what the documentation says (to me).
More information about the fpc-pascal
mailing list