Copy const array to dynamic array in Delphi
Asked Answered
D

3

10

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 = . . . . .
Dunker answered 11/6, 2009 at 17:7 Comment(0)
W
12
function CopyByteArray(const C: array of Byte): TByteDynArray;
begin
  SetLength(Result, Length(C));
  Move(C[Low(C)], Result[0], Length(C));
end;

procedure TFormMain.Button1Click(Sender: TObject);
const
  C: array[1..10] of Byte = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var
  D: TByteDynArray;
  I: Integer;
begin
  D := CopyByteArray(C);
  for I := Low(D) to High(D) do
    OutputDebugString(PChar(Format('%d: %d', [I, D[I]])));
end;

procedure TFormMain.Button2Click(Sender: TObject);
const
  C: array[1..10, 1..10] of Byte = (
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

var
  D: array of TByteDynArray;
  I, J: Integer;
begin
  SetLength(D, Length(C));
  for I := 0 to Length(D) - 1 do
    D[I] := CopyByteArray(C[Low(C) + I]);

  for I := Low(D) to High(D) do
    for J := Low(D[I]) to High(D[I]) do
      OutputDebugString(PChar(Format('%d[%d]: %d', [I, J, D[I][J]])));
end;
Whiff answered 11/6, 2009 at 17:37 Comment(0)
M
26

This will copy constAry1 to dynAry.

SetLength(dynAry, Length(constAry1));
Move(constAry1[Low(constAry1)], dynAry[Low(dynAry)], SizeOf(constAry1));
Monstrous answered 11/6, 2009 at 17:34 Comment(4)
I believe both answers are correct, so I am going to accept TOndrej's because it is more complete, not that yours is wrong (I up voted it).Dunker
Should this be SizeOf() or Length(constAry)?Harney
Actually, for consistency you should use either SizeOf or Length in both lines.Spirograph
@Spirograph Actually, no. First line needs to correctly set number of elements. Second line needs to move number of bytes. So first line needs Length and second line needs SizeOf (and that SizeOf only works because the source array is static, not dynamic).Monstrous
W
12
function CopyByteArray(const C: array of Byte): TByteDynArray;
begin
  SetLength(Result, Length(C));
  Move(C[Low(C)], Result[0], Length(C));
end;

procedure TFormMain.Button1Click(Sender: TObject);
const
  C: array[1..10] of Byte = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var
  D: TByteDynArray;
  I: Integer;
begin
  D := CopyByteArray(C);
  for I := Low(D) to High(D) do
    OutputDebugString(PChar(Format('%d: %d', [I, D[I]])));
end;

procedure TFormMain.Button2Click(Sender: TObject);
const
  C: array[1..10, 1..10] of Byte = (
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

var
  D: array of TByteDynArray;
  I, J: Integer;
begin
  SetLength(D, Length(C));
  for I := 0 to Length(D) - 1 do
    D[I] := CopyByteArray(C[Low(C) + I]);

  for I := Low(D) to High(D) do
    for J := Low(D[I]) to High(D[I]) do
      OutputDebugString(PChar(Format('%d[%d]: %d', [I, J, D[I][J]])));
end;
Whiff answered 11/6, 2009 at 17:37 Comment(0)
G
2

From Delphi XE7, the use of string-like operations with arrays is allowed. Then you can declare a constant of a dynamic array directly. For example:

const
  KEY: TBytes = [$97, $45, $3b, $3e, $c8, $14, $c9, $e1];
Gertiegertrud answered 7/5, 2020 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.