DelphiBasics
$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
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Trapping array bound problems
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page