Does copy on write semantics applies for dynamic arrays of records?
How to properly duplicate an array of record?
Is this enough?:
type
recordA = Record
Y:integer;
end;
var x: array of recordA;
b: array of recordA;
item: recordA;
begin
SetLength(x, 2);
item.Y:= 2;
x[0] := item;
item.Y:= 5;
x[1] := item;
//Copying
b:= x;
After the copy is complete, I'll need to reset the first array:
SetLength(x, 0);
May I do it this way?
Dynamic arrays do not support Copy-on-Write (CoW) semantics. It does not matter in you example but it matters in other cases.
If you need to copy the contents of a dynamic array use Copy
function. Here is an example demonstrating the difference between dynamic array assignment and copying:
procedure TestCopy;
type
recordA = Record
Y:integer;
end;
arrayA = array of recordA;
var x, b, c: arrayA;
item: recordA;
begin
SetLength(x, 2);
item.Y:= 2;
x[0] := item;
item.Y:= 5;
x[1] := item;
b:= x;
x[0].Y:= 4;
Writeln(b[0].Y, ' -- ', x[0].Y);
b:= Copy(x);
x[0].Y:= 8;
Writeln(b[0].Y, ' -- ', x[0].Y);
end;