[fpc-pascal] 2D Dynamic arrays and BlockRead

Martin fpc at mfriebe.de
Mon Dec 6 00:45:14 CET 2010


On 05/12/2010 23:35, andrew.bennett at ns.sympatico.ca wrote:
>
> J?rgen Hestermann<juergen.hestermann at gmx.de>  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).
>
> After wasting another 2 days reading the documentation, I returned to
> blind experiment ...
>
> I find that BlockRead(F, A[J,0], RightDim*Sizeof(Single), count) works where
> BlockRead(F, A[J], RightDim*Sizeof(Single), count) does not.
> That is, reading RightDim elements into a space the size of one element
> is OK but reading them into a space of the correct size is not.
>
> Personally, I would call that a BUG.
No, a feature.

I guess you assume that an array "a array of array of integer" is a 
continuous block in memory. But it is not

type TIntArray: array of integer;
var a1: TIntArray;

makes "a1" a *pointer* to a block of memory

var a2: array of TIntArray;

makes a2 a pointer to a list of pointers, each of them pointing to a 
further block of memory.

BlockRead(F, A[J], ....)
overwrites the pointers, pointing to the 2nd dimension arrays.

BTW, it actually is not just a pointer, but a refcounted pointer.
so after BlockRead(F, A[J], ....); not only do the pointer point to random location, the whole refcounting is messed up too.

Martin



More information about the fpc-pascal mailing list