Sign in Join
Wiki copytemporary

$rangeChecks: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-The [[FPC]]’s [[local compiler directives|local compiler directive]] '''
2-
3-```delphi
4-{$rangeChecks}
5-```
6-
7-''' enables or disable range checks of ordinal data type expressions, the default being
8-
9-```delphi
10-{$rangeChecks off}
11-```
12-
13-. The short form
14-
15-```delphi
16-{$R+}
17-```
18-
19-and
20-
21-```delphi
22-{$R-}
23-```
24-
25-is recognized too.
1+The [[FPC]]’s [[local compiler directives|local compiler directive]] **`{$rangeChecks}`** enables or disable range checks of ordinal data type expressions, the default being `{$rangeChecks off}`. The short form `{$R+}` and `{$R-}` is recognized too.
262
273## range checks
284
295A range check ensures a value is within the domain of the destination operand. As soon as an operand needs to be “squeezed” into a smaller range, the expression or statement is eligible to a range check.
306
317### behavior
328
339If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample
3410
3511```delphi>
3612 {$rangeChecks on}
3713 x := y;
3814</syntaxhighlight>
3915is ''semantically'' equivalent to
4016<syntaxhighlight lang=
4117```
4218
4319`   {$rangeChecks off}`\
4420`   x := y;`\
4521`   if (x < low(x)) or (x > high(x)) then`\
4622`   begin`\
4723`       runError(201);`\
4824`   end;`
4925
5026</syntaxhighlight>
5127
5228(The reported memory address at which the error occurred will differ and the compiler may produce optimized code.)
5329
5430### eligibility
5531
5632Range checks can be performed
5733
58-- in [[Becomes|assignments]] (
59- ``` pascal
60- :=
61- ```
62-
63- ), and more specifically
64-
34+- in [[Becomes|assignments]] (`:=`), and more specifically
6535 - when passing actual parameters, or
6636 - indicating [[Array|array]] indices, but also
6737- when doing [[Typecast|typecasts]].
6838
6939For assignments involving constant expressions a range check is performed already at [[compile-time error|compile-time]], but is either a warning or error depending on the current setting.
7040
7141### placement
7242
7343The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level.
7444
7545## caveats
7646
77-- It is important to remember that in [[Pascal]] *arithmetic* [[expression|expressions]] are always evaluated on a computer’s “native” (signed) integer data type. If the destination operand of an arithmetic expression is, for instance a
78- ```delphi
79- ALUSInt
80- ```
81-
82- , this will never trigger a runtime error.
47+- It is important to remember that in [[Pascal]] *arithmetic* [[expression|expressions]] are always evaluated on a computer’s “native” (signed) integer data type. If the destination operand of an arithmetic expression is, for instance a `ALUSInt`, this will never trigger a runtime error.
8348- If range checks are meant to be performed when passing parameters, range checks must be enabled *at* the call site.
8449
8550## application
8651
87-Range checks can incur a significant performance penalty. Therefore, the FPC disables range checks by default. However, at least *during* development and prior releases of software, range checks should be enabled to find “obvious” programming mistakes. As a consequence of this practice, if a code fragment is *meant to* exceed the permissible range, it should be documented like this: \<syntaxhighlight lang="delphi\>
88-
89-`   {$push}`\
90-`       {$rangeChecks off}`\
91-`       x := y;`\
92-`   {$pop}`
52+Range checks can incur a significant performance penalty. Therefore, the FPC disables range checks by default. However, at least *during* development and prior releases of software, range checks should be enabled to find “obvious” programming mistakes. As a consequence of this practice, if a code fragment is *meant to* exceed the permissible range, it should be documented like this:
9353
54+```delphi>
55+ {$push}
56+ {$rangeChecks off}
57+ x := y;
58+ {$pop}
9459</syntaxhighlight>
9560
96-Use
97-
98-```delphi
99-{$ifOpt}
61+Use <code>{$ifOpt}</code> if code needs to be different on the current range-check-setting:
62+<syntaxhighlight lang=
10063```
101-
102-if code needs to be different on the current range-check-setting: \<syntaxhighlight lang="delphi\>
10364
10465`   {$ifOpt R-} // or R+`\
10566`       …`\
10667`   {$endIf}`
10768
10869</syntaxhighlight>
10970
11071---
11172*From the Free Pascal wiki, page "$rangeChecks", as it stood on 2022-03-05 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
11273Category: FPC

Back to history