[fpc-pascal] Dynamic array question

Vojtěch Čihák vojtech.cihak at atlas.cz
Wed Jan 11 23:38:19 CET 2023


Hi,
 
is there a way how to have dynamic array "inside" another dynamic array?
 
program project1;
{$mode objfpc}{$H+}
 
uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes, SysUtils
  { you can add units after this };
 
{$R *.res}
type
  TDynArray = array of Integer;
  PTDynArray = ^TDynArray;
 
var aArray, aNewArray: TDynArray;
    aNewStart: Integer;
begin
  SetLength(aArray, 300);
  writeln('Length of aArray ', length(aArray));
  aNewStart:=100;
  aArray[aNewStart+5]:=42;
  aNewArray:=@aArray[aNewStart];
  aNewArray[5]:=42;
  writeln('Length of aNewArray ', length(aNewArray));
end.      
 
So aNewArray begin at aArray[100] and have length=200 and I could write
NewArray[5]:=42; instead of aArray[aNewStart+5]:=42;
After all, memory is allocated correctly.
Code above gives "Invalid pointer operation" while this code
 
  aNewArray:=PTDynArray(@aArray[aNewStart])^;
 
  aNewArray[5]:=42; 
 
gives "Range check error".
 
Thanks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230111/f56f8efe/attachment.htm>


More information about the fpc-pascal mailing list