[fpc-pascal] dynamic arrays: copy-on-write or not?

David Emerson dle3ab at angelbase.com
Thu Feb 5 05:55:35 CET 2009


From the reference guide, december 2008 version:

"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."

This is fine, but what is strange is that calling setlength DOES perform 
copy-on-write!

var a, b : array of longint;
begin
  setlength (a,2);
  b := a;
  setlength (a,5);
  writeln (length(b));   // I get 2, not 5
end.


Why this inconsistency? Seems like the behaviors for assigning an 
element and for using setlength should be similar.

Cheers,
David




More information about the fpc-pascal mailing list