1

Static arrays allow to define their low and high bounds:

StaticArray : array[5..7] of Integer;

I don't know how to do the same thing with dynamic arrays at runtime.

For example, if I need to copy the elements from a static array, keeping the same indexes, I don't know how to set its low bound to Low(StaticArray) and high bound to High(StaticArray):

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  //?

  //copying elements
  i := 0;
  for i := Low(StaticArray) to High(StaticArray) do
    DynamicArray[i] := StaticArray[i];
end;

Is there any way for setting the low/high bounds of a dynamic array, or do they always have 0 as low bound and Length(Array) - 1 as high bound?

1
  • Why do you want it? I understand that lower and upper bound is information on its own (i.e. have a scale of 1.. 6 instead of 0.. 5), but that would be as trivially pointless as distinguishing between fixed point decimals and plain integers. Commented Mar 2, 2021 at 21:16

2 Answers 2

7

Every time you have a question about the Delphi language, you should consult the official documentation. In this case, the section named Dynamic Arrays on Structured Types says the following:

Dynamic arrays are always integer-indexed, always starting from 0.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there any way for setting the low/high bounds of a dynamic array

No.

or do they always have 0 as low bound and Length(Array) - 1 as high bound?

Yes. Which means you can do this:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  for i := Low(StaticArray) to High(StaticArray) do
    DynamicArray[i-Low(StaticArray)] := StaticArray[i];
end;

Or, just use two separate index variables:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
  i, j : integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  j := 0;
  for i := Low(StaticArray) to High(StaticArray) do begin
    DynamicArray[j] := StaticArray[i];
    Inc(j);
  end;
end;

Or, since your arrays contain non-managed trivial types, just replace the whole loop with a single Move() call instead:

var
  StaticArray : array[5..7] of Integer;
  DynamicArray : array of Integer;
begin
  //initializing static array
  StaticArray[5] := 1;
  StaticArray[6] := 2;
  StaticArray[7] := 3;

  //setting the same length and bounds to the dynamic array
  SetLength(DynamicArray, Length(StaticArray));

  //copying elements
  Move(StaticArray[Low(StaticArray)], DynamicArray[0], Length(DynamicArray) * SizeOf(Integer));
end;

Comments

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.