Sign in Join
Wiki copytemporary

$rangeChecks: what changed

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

OldNewLine
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.
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.
22
3+## Contents
4+
5+ * 1 range checks
6+ * 1.1 behavior
7+ * 1.2 eligibility
8+ * 1.3 placement
9+ * 2 caveats
10+ * 3 application
11+
312## range checks
413
5-A 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.
14+A 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.
615
716### behavior
817
9-If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample
10-
11-```delphi>
12- {$rangeChecks on}
13- x := y;
14-</syntaxhighlight>
15-is ''semantically'' equivalent to
16-<syntaxhighlight lang=
18+If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample
1719```
18-
19-`   {$rangeChecks off}`\
20-`   x := y;`\
21-`   if (x < low(x)) or (x > high(x)) then`\
22-`   begin`\
23-`       runError(201);`\
24-`   end;`
20+{$rangeChecks on}
21+ x := y;
22+```
2523
26-</syntaxhighlight>
24+is _semantically_ equivalent to
25+```
26+{$rangeChecks off}
27+ x := y;
28+ if (x < low(x)) or (x > high(x)) then
29+ begin
30+ runError(201);
31+ end;
32+```
2733
28-(The reported memory address at which the error occurred will differ and the compiler may produce optimized code.)
34+(The reported memory address at which the error occurred will differ and the compiler may produce optimized code.)
2935
3036### eligibility
3137
32-Range checks can be performed
38+Range checks can be performed
3339
34-- in [[Becomes|assignments]] (`:=`), and more specifically
35- - when passing actual parameters, or
36- - indicating [[Array|array]] indices, but also
37-- when doing [[Typecast|typecasts]].
40+ * in [[Becomes|assignments]] (`:=`), and more specifically
41+ * when passing actual parameters, or
42+ * indicating [[Array|array]] indices, but also
43+ * when doing [[Typecast|typecasts]].
3844
39-For 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.
45+For 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.
4046
4147### placement
4248
43-The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level.
49+The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level.
4450
4551## caveats
4652
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.
48-- If range checks are meant to be performed when passing parameters, range checks must be enabled *at* the call site.
53+ * 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.
54+ * If range checks are meant to be performed when passing parameters, range checks must be enabled _at_ the call site.
4955
5056## application
5157
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:
53-
54-```delphi>
55- {$push}
56- {$rangeChecks off}
57- x := y;
58- {$pop}
59-</syntaxhighlight>
60-
61-Use <code>{$ifOpt}</code> if code needs to be different on the current range-check-setting:
62-<syntaxhighlight lang=
58+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:
6359```
64-
65-`   {$ifOpt R-} // or R+`\
66-`       …`\
67-`   {$endIf}`
60+{$push}
61+ {$rangeChecks off}
62+ x := y;
63+ {$pop}
64+```
6865
69-</syntaxhighlight>
66+Use `{$ifOpt}` if code needs to be different on the current range-check-setting:
67+```
68+{$ifOpt R-} // or R+
69+ …
70+ {$endIf}
71+```
7072
7173---
72-*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).*
73-Category: FPC
74+*From the Free Pascal wiki, page "$rangeChecks", as the Internet Archive kept it on 2024-09-19. 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).*

Back to history