[fpc-pascal] Accessing open array

Jonas Maebe jonas.maebe at elis.ugent.be
Sun Jun 3 18:15:47 CEST 2012


On 03 Jun 2012, at 17:53, Koenraad Lelong wrote:

> Im trying to access an open array but I get a runtime error.

You are trying to use a pointer to a static array as a pointer to a dynamic array (not an open array; have a look at e.g. http://rvelthuis.de/articles/articles-openarr.html to read about the differences between all the kinds of array types).

It's normal that this crashes. A static array is something completely different than a dynamic array. If you add {$T+} to your source code (to enable typed pointers), the compiler will tell you that those pointer types are incompatible.

> Any suggestions how I can correct my code ?


It is not possible to declare an initialized dynamic array. You have to initialize it dynamically, e.g. via

var
  arr: tarray;
begin
  setlength(arr,8);
  move(FONT6x8[low(FONT6x8)],arr[low(arr)],8);
end;

Note: the extra [low(arr)] is required for the dynamic array (because a dynamic array is an implicit pointer, and without the [low(arr)] you will start overwriting that implicit pointer itself, along with everything coming after it in memory). It's not required for the static array, but it's good practice to always write it in order to avoid getting nasty bugs when changing the type of the array later on for some reason. And using low() rather than 0 protects you in case the lower bound of the array changes for some reason.


Jonas


More information about the fpc-pascal mailing list