SmartPascal
$R
Compiler Directive
Determines whether Delphi checks array bounds
1   {$R FileName}
2   {$R-}
3   {$R+}
Description
The $R compiler directive has two forms. Firstly, it defines a resource file to be compiled. Secondy, it determines whether Delphi should add code for array bounds checking.
 
Version 1
 
Defines a resource file. refer to $Resource for further information.
 
Versions 2 and 3 This is set off (-) by default, meaning that a bad array access will pass unnoticed, revealing itself in a difficult to debug part of the code.
 
It is recommended to switch on $R in order to detect array bound problems. This will result in the raising of an exception, allowing code testing to correctlt identify the point of failure.
Notes
$R FileName is equivalent to $Resource FileName.
$R- is equivalent to $RangeChecks Off.
$R+ is equivalent to $RangeChecks On.

It can and should only be set once in your code.

The default value is $R-.
Related commands
$RangeChecks Determines whether Delphi checks array bounds
$Resource Defines a resource file to be included in the application linking
Array A data type holding indexable collections of data
 
Example code : Trapping array bound problems
var
  myArray : array[1..5] of string;
  i : Integer;

begin
  // Set range checking on
  {$R+}

  // Start array assignment from 0 - normally OK, but our
  // array starts at 1.
  for i := 0 to 5 do
  begin
    myArray[i] := 'Element '+IntToStr(i);
    ShowMessage('myArray['+IntToStr(i)+'] = '+myArray[i]);
  end;
end;
Show full unit code
   Delphi throws the ERangeError exception
 
Example code : Ignoring array bounds problems
var
  myArray : array[1..5] of string;
  i : Integer;

begin
  // Set range checking off
  {$R-}

  // Start array assignment from 0 - normally OK, but our
  // array starts at 1.
  for i := 0 to 5 do
  begin
    myArray[i] := 'Element '+IntToStr(i);
    ShowMessage('myArray['+IntToStr(i)+'] = '+myArray[i]);
  end;
end;
Show full unit code
   myArray[0] = Element 0
   myArray[1] = Element 1
   myArray[2] = Element 2
   myArray[3] = Element 3
   myArray[4] = Element 4
   myArray[5] = Element 5