[fpc-pascal] Dynamic arrays, yet another pitfall

Jürgen Hestermann juergen.hestermann at gmx.de
Sun Feb 9 18:14:05 CET 2014


Am 2014-02-09 16:34, schrieb Flávio Etrusco:
 > In other words: dynamic arrays are like AnsiStrings without the
 > copy-on-write semantics. I'd certainly wish Borland copied the COW
 > semantics :-/

Yes. Therefore the issue that I described for dynamic arrays cannot occur for ansistrings. But COW can also be a drawback. Imagine an ansistring within a record of a heavily pointer-connected deeply nested data structure and I want to write a function that gives me back a reference to this string:

---------------------------------
function FindMyStringToModify : AnsiString;
...
var A : AnsiString;
A := FindMyStringToModify;
A := 'New String';
---------------------------------

then the change to A will not affect the ansistring in the nested data structure (because of COW).
But this is possible for dynamic arrays *if* they are not empty.




More information about the fpc-pascal mailing list