I think that the following program ought to produce a run-time error but it doesn't. I have had a look at the GPC manual but I can't find any compiler directive that says Check array bounds, and I did rather think that the philosophy of Pascal was to prevent this sort of thing happening without the user having to take any special action. So, is there a way of generating the code that says 'Check array bounds' (I know other dialects of Pascal that do this by default).
Version 2.95.2 19991024 (release), running under RedHat 6.1, Cartman. Yes, I know it's old, but my version of VMWare won't work on the later kernels. ----------------- program arraytest(output); var i:integer; a,b:array[1..5] of integer; begin for i:=1 to 5 do a[i]:=i; for i:=1 to 10 do b[i]:=a[i]; for i:=1 to 20 do writeln(i,' ',b[i]); end. ------------------- The results file follows: -----------
1 1 2 2 3 3 4 4 5 5 6 1 7 2 8 3 9 4 10 5 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0
---------
John O.
John Ollason wrote:
I think that the following program ought to produce a run-time error but it doesn't. I have had a look at the GPC manual but I can't find any compiler directive that says Check array bounds, and I did rather think that the philosophy of Pascal was to prevent this sort of thing happening without the user having to take any special action. So, is there a way of generating the code that says 'Check array bounds' (I know other dialects of Pascal that do this by default).
Not yet. It will be in one of the next versions.
Frank
John Ollason wrote:
I think that the following program ought to produce a run-time error but it doesn't. I have had a look at the GPC manual but I can't find any compiler directive that says Check array bounds,
... snip ...
Version 2.95.2 19991024 (release), running under RedHat 6.1, Cartman. Yes, I know it's old, but my version of VMWare won't work on the later kernels.
program arraytest(output); var i:integer; a,b:array[1..5] of integer; begin for i:=1 to 5 do a[i]:=i; for i:=1 to 10 do b[i]:=a[i]; for i:=1 to 20 do writeln(i,' ',b[i]); end.
This has to do with the lack of bounds checking in gpc, so far. However you are not giving your code the best change of detecting problems. Note the minor revision below:
PROGRAM arraytest(output); CONST alimit = 5; TYPE aix = 1..alimit; VAR i : aix; a, b : ARRAY[aix] OF integer; BEGIN FOR i := 1 TO alimit DO a[i] := i; FOR i := 1 TO 10 DO b[i] := a[i]; FOR i := 1 TO 20 DO writeln(i, ' ', b[i]); END.