Jacek Krawczyk
Jacek Krawczyk

Reputation: 2184

Convert dynamic array to static

Because of compatibility purpose, I need to pass my dynamic array (of Double) to the obsolete type p_vektor defined as

p_vektor=array[1..50] of double;

Although the both declarations differ from the first index declaration, the used type is the same. I wrote the following code to copy the arrays, but it fails.

function DoubleArrayToPVektor(const aArray: TArray<Double>): p_vektor;
begin
  Move(aArray[Low(aArray)], Result[Low(Result)], Length(aArray));
end;

As the result, only the first element is copied successfully. Is there any quick way to copy the array without using an alternative, iterative solution?

Upvotes: 3

Views: 1301

Answers (1)

David Heffernan
David Heffernan

Reputation: 613192

Move accepts the number of bytes. You pass the number of elements. You need to pass

Length(aArray)*SizeOf(aArray[0])

as the number of bytes to be copied.

Do make sure that you check that the length of aArray is no more than 50.

Also be aware that you are not initialising all elements of the result array. Whether that matters only you can judge.

Upvotes: 4

Related Questions