Search code examples
delphipointersdynamic-arrayslazarusfreepascal

How to obtain an array from type TByteArray as part of a larger array from type TByteArray?


I define a large typed dynamic array aArray. And now want to have a second array bArray marking a certain area in aArray without beeing a copy!

type
  TByteArray = array of Byte;

implementation

procedure SomeCode;
var 
  aArray : TByteArray;
  bArray : TByteArray;
begin
  setlength(aArray, 30);
  aArray[4] := 12;
  // here want to have bArray to hold 20 Bytes starting from Byte 5 in aArray 
  if bArray[0] = aArray[4] then begin
    writeln('All OK'); 
  end;

end;

Solution

  • This is not possible. A dynamic array contains meta data located immediately before the first element of the array. That's not compatible with you wanting a dynamic array whose first element is in the middle of another array.

    You will probably need to solve your problem using a pointer to the first element of the sub array.