[fpc-pascal] Copy dynamic array

denisgolovan denisgolovan at yandex.ru
Wed May 16 10:24:16 CEST 2018


Doing the same conceptual thing using different syntax does not seem right as generics, macros and other nice stuff become clumsy and too verbose.

See

//========================================================
program project1;
{$mode objfpc}

type
   TRec= record
      A:array of integer;
      S:string;
   end;

var R1,R2:TRec;
begin
   SetLength(R1.A, 3);
   R1.A[0]:=1;
   R1.A[1]:=2;
   R1.A[2]:=3;
   R1.S:='123';

   R2:=R1; // shallow copying <> full clone
   R2.A[0]:=10;
   R2.S[1]:='S';

   // does not work as expected (no copy-on-write/cloning)
   writeln(R1.A[0]); // prints 10
   writeln(R2.A[0]); // prints 10

   // works fine
   writeln(R1.S[1]); // prints 1
   writeln(R2.S[1]); // prints S
end.
//========================================================

BR,
Denis



More information about the fpc-pascal mailing list