SSJX.CO.UK
Browser Games | Cybiko | Amiga | Applications | Windows Games | Java Area | Test Area | Guides | Site Map

Out Of Bounds - Pascal

Introduction

This is part of a series testing each of the languages I use to see how good they are at catching simple array out of bounds errors.

These tests check for the following:

The Code

The following was tested using the FreePascal Compiler 3.0.4 :

program oob;

// These are checks are commented out for Test 1
{$J-} // Constants are Constant
{$R+} // Range Checking

var
	i:integer;
	pa:integer=3;
	myarray:array[0..2] of integer=(1,2,3);

const
	pb:integer=4;

begin	
	// #1. Simple so should be an easy catch - CT: CAUGHT
	myarray[3]:=1;

	// #2. This is a constant so should also be an easy catch - CT: FAILED
	myarray[pb]:=99;
	
	// #3. Variable index but does start outside - CT: FAILED
	myarray[pa]:=98;

	// #4. Looping through array - CT: FAILED
	for i:=0 to 4 do
	begin
		writeln(myarray[i]);
	end;
end.

Test 1 - Default Settings

By default the error #1 is caught, but I am a bit disappointed it did not pick up any of the others.

With #1 commented out, the program runs and displays 1,2,3,98,99.

Test 2 - Checks Enabled

With the checks enabled, when the program is run a runtime error is given for each of the errors.

Summary

These results were not as good as I had expected. The main lesson is to add range checking using {$R+} to your programs!

11/01/20