0

I'm trying to copy all the items from a generic array to a new one using TArray.Copy (Note: In Delphi XE7 the function has no documentation).

class procedure Copy<T>(const Source, Destination: array of T; SourceIndex, DestIndex, Count: NativeInt); overload; static;
class procedure Copy<T>(const Source, Destination: array of T; Count: NativeInt); overload; static;

The Source array turns into a zero filled array after executing the function:

var
  Source : TArray<Integer>;
  Destination : TArray<Integer>;
begin
  Source := [10, 20];
  SetLength(Destination, Length(Source));

  //here Source is [10, 20]

  TArray.Copy<Integer>(Source, Destination, 0, 0, Length(Source));

  //here Source is [0, 0]
end;

Why is it changing the Source array?

3
  • Can't reproduce in Delphi 10. Commented Jul 28, 2022 at 13:46
  • 1
    Are you inspecting the source array contents in Delphi IDE or in code? If in Delphi IDE then the result for this might be code optimization since after the array copy procedure is made source array no longer need to exist. Commented Jul 28, 2022 at 14:54
  • @SilverWarior: Good point, unfortunately it also happens when using the array values after checking them Commented Jul 29, 2022 at 6:21

1 Answer 1

3

TArray.Copy was broken in XE7 because someone confused the parameters of System.Move:

RSP-9763: TArray.Copy copies from destination to source for unmanaged types

RSP-9887: TArray.Copy is broken in multiple ways

FWIW, for your scenario, it does not need TArray.Copy - System.Copy can handle copying arrays if the copied range starts from the beginning. If you want to copy the entire content, you can even omit the second and third parameter and simply call:

Destination := Copy(Source);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for informations, System.Copy works fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.