[fpc-pascal] Copy dynamic array

Alexander Grotewohl alex at dcclost.com
Wed May 16 17:39:40 CEST 2018


What would the expected behavior be for pointers inside records? What if 
the record is actually a linked list? Or includes classes and objects? 
Do we run the constructor or no? If the record has file handles do we 
attempt to recreate their state? (perhaps running assign again, and 
crossing our fingers the file hasn't been deleted - which at least on 
Linux is definitely possible)

On 05/16/2018 04:24 AM, denisgolovan wrote:
> 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
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list