[fpc-pascal] Dynamic array question

Joost van der Sluis joost at cnoc.nl
Thu Jan 12 00:15:15 CET 2023


Vojtěch Čihák via fpc-pascal schreef op wo 11-01-2023 om 23:38 [+0100]:
> is there a way how to have dynamic array "inside" another dynamic
> array?

Not in the way you want.

You could define an 'array of array', though. Or an array of records
that contain an 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".

The problem is that you make all kind of assumptions about the memory-
layout of these arrays, which may not always be valid. Code that works
this way might work coincidentally, but may break when switching to
another compiler version or by enabling an optimization. 

So.. you must really, really have a good reason why you should try
this.  And if you do so, don't use dynamic types for tricks like this.
They are not meant to be used as a a way to map directly into a memory
location.

And, in your case, one of the assumptions is wrong. At the start of a
dynamic array it's length is stored. That will destroy your memory-
layout.

And te compiler will try to allocate/deallocate memory for the second
array. One big mess.

You could use a static array for the 'inside' array.

Regards,

Joost.


More information about the fpc-pascal mailing list