Copy const array to dynamic array in Delphi

Jim McKeeth picture Jim McKeeth · Jun 11, 2009 · Viewed 14.1k times · Source

I have a fixed constant array

constAry1: array [1..10] of byte = (1,2,3,4,5,6,7,8,9,10);

and a dynamic array

dynAry1: array of byte;

What is the easiest way to copy the values from constAry1 to dynAry1?

Does it change if you have a const array of arrays (multidimensional)?

constArys: array [1..10] of array [1..10] of byte = . . . . .

Answer

gabr picture gabr · Jun 11, 2009

This will copy constAry1 to dynAry.

SetLength(dynAry, Length(constAry1));
Move(constAry1[Low(constAry1)], dynAry[Low(dynAry)], SizeOf(constAry1));