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

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Feb 5 10:31:05 CET 2009

On 05 Feb 2009, at 05:55, David Emerson wrote:

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

It's the way Delphi works. In fact, calling setlength is /the/ way to  
make a dynamic array unique (afaik, the only alternative is calling  
copy, but that's slower in case the array already was unique.


More information about the fpc-pascal mailing list