[fpc-pascal] Dynamic array question

Vojtěch Čihák vojtech.cihak at atlas.cz
Thu Jan 12 16:52:31 CET 2023


Thanks for comprehensive answer. My other code really worked coincidentally.
This code:
 
  SetLength(aArray, 400);
  writeln(PInt64(@aArray[0]-8)^);
 
writes out 399.
 
I'll use more conservative code.
 
V.
______________________________________________________________
> Od: "Joost van der Sluis via fpc-pascal" <fpc-pascal at lists.freepascal.org>
> Komu: fpc-pascal at lists.freepascal.org
> Datum: 12.01.2023 00:22
> Předmět: Re: [fpc-pascal] Dynamic array question
>
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.
  
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.
 _______________________________________________
 fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
 https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>

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


More information about the fpc-pascal mailing list