[fpc-pascal] 2D Dynamic arrays and BlockRead

Marc Weustink marc at dommelstein.net
Mon Dec 6 23:43:04 CET 2010

On 3-12-2010 17:26, J├╝rgen Hestermann wrote:
> andrew.bennett at ns.sympatico.ca schrieb:
>> After using BlockRead to fill a 2D dynamic array, I get an access
>> violation on the very first reference. A 2D array with only one
>> dimension dynamic works OK.
>> What am I missing?
> Maybe you blundered into the same trap as so many others who do not know
> that dynamic arrays are *pointers* (to arrays). It is one of the sins
> done by Borland to abandon the once strict logic that in Pascal the
> syntax is always context independend. Now this is no longer the case
> (i.e. for dynamic arrays).
> in your example
> STat = Array[0..W-1] Of Single ; { Static array }
> DST = Array Of STat ; { One dimension dynamic, the other static }
> D2T = Array Of Array Of Single ; { Two dynamic dimensions }
> STat always means the address starting with STat[0] (context independend).
> Also DST always means the address where DST[0].

Nope, there is a difference between DST and DST[0]. DST won't give you 
the first element. You can try this youself with an untyped parameter:

procedure Foo(const AParam);

You will see a difference between passing DST or DST[0]

To be safe, for any N-dimension dynamic array always use [0,..,0]  to 
pass the first element. To avoid confusion, you can do this for static 
arrays too.

> But D2T is a pointer which can be either the the address of the pointer
> (in all low level routines like fillchar, sizeof, BlockRead/-Write etc.)
> but also can be the address of the D2T[0] because you can refer to
> elements without the need to dereference it as in D2T^[0].

In behaviour DST and D2T wont differ.


More information about the fpc-pascal mailing list