The Delphi Bug List

Entry No.
680
RTL - Sys - System
Copy() does not work as expected with dynamic Arrays more than 1 dimension
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
N/AN/AN/AN/AN/AGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotcha
Description
Reported by Arsène von Wyss; checked by Jordan Russell
The Copy() function can be used to create a (unique) copy of a string or dynamic array:
    Dst:=Copy(Src);
However, when Src has more than one dimension, Delphi will only create a unique copy of the first dimension instead of the whole dynamic array. This is by design but nonetheless confusing.
Solution / workaround
Use SetLength if the array if all arrays of one dimension have the same size.

If the sizes are different, use Copy() on every array of the most inner dimension. For 2D, this would be like this:

    Src,Dst: array of array of Integer;

    SetLength(Dst,Length(Src));
    for I:=Low(Dst) to High(Dst) do
        Dst[I]:=Copy(Src[I]);
For 3D:
    Src,Dst: array of array of array of Integer;

    SetLength(Dst,Length(Src));
    for I:=Low(Dst) to High(Dst) do begin
        SetLength(Dst[I],Length(Src[I]));
        for J:=Low(Dst[I]) to High(Dst[I]) do
            Dst[I][J]:=Copy(Src[I][J]);
    end;
Latest update of this entry: 2001-08-28

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.