[fpc-pascal] 2D Dynamic arrays and BlockRead
andrew.bennett at ns.sympatico.ca
andrew.bennett at ns.sympatico.ca
Fri Dec 3 02:32:15 CET 2010
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?
Windows, FPC 2.2.2, 2.2.4 or 2.4.2.
Program WriteA ; { Write a binary file using using static array and BlockWrite }
Const W = 2 ; H = 2 ; FName = 'C:\FPC\2.2.2\USR\GASH.BIN' ;
Type A2T = Array[0..H-1,0..W-1] Of Single ;
Var D : A2T ; F : File ; J, C : Longint ;
Begin
D[0,0] := 1 ; D[0,1] := 2 ; D[1,0] := 3 ; D[1,1] := 4 ;
Assign(F, FName) ; Rewrite(F, 1) ;
For J := 0 To H-1 Do Blockwrite(F, D[J], W*Sizeof(Single), C) ;
Close(F) ;
Writeln(D[0,0]:2:0, #9, D[0,1]:2:0, #9, D[1,0]:2:0, #9, D[1,1]:2:0) ;
End.
Program DynRd ; { Read binary file written by "WriteA" }
Const W = 2 ; H = 2 ; FName = 'C:\FPC\2.2.2\USR\GASH.BIN' ;
Type
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 }
Var DD : D2T ; DS : DST ; F : File ; J, C : Longint ;
Begin
SetLength(DS, H) ; { Test mixed array }
Assign(F, FName) ;
Reset(F, 1) ;
For J := 0 To H-1 Do Begin
BlockRead(F, DS[J], W*Sizeof(Single), C) ;
If (C <> W*Sizeof(Single)) Then Writeln('Oops at J = ', J) ;
End ;
Close(F) ;
Writeln(DS[0,0]:2:0, #9, DS[0,1]:2:0, #9, DS[1,0]:2:0, #9, DS[1,1]:2:0,
#9'Should be 1, 2, 3, 4') ; { This works }
SetLength(DS, 0) ;
SetLength(DD, H, W) ; { Test 2D dynamic array }
Assign(F, FName) ; Reset(F, 1) ;
For J := 0 To H-1 Do Begin
BlockRead(F, DD[J], W*Sizeof(Single), C) ;
If (C <> W*Sizeof(Single)) Then Writeln('Oops at J = ', J) ;
End ;
Close(F) ;
Writeln(DD[0,0]:2:0, #9, DD[0,1]:2:0, #9, DD[1,0]:2:0, #9, DD[1,1]:2:0,
#9'Should also be 1, 2, 3, 4') ; { Quits, error 216, reading DD[0,0] }
SetLength(DD, 0, 0) ;
End.
More information about the fpc-pascal
mailing list