Sign in Join
Wiki copytemporary

$rangeChecks: what changed

Revision 3 to 4. 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.
2-
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
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.
112
123## range checks
134
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.
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.
156
167### behavior
178
18-If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample
19-```
20-{$rangeChecks on}
21- x := y;
22-```
9+If a range check fails, the [[runtime error]] 201 “Range check error” is generated. Thus the code sample
2310
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;
11+```delphi>
12+ {$rangeChecks on}
13+ x := y;
14+</syntaxhighlight>
15+is ''semantically'' equivalent to
16+<syntaxhighlight lang=
3217```
3318
34-(The reported memory address at which the error occurred will differ and the compiler may produce optimized code.)
19+`   {$rangeChecks off}`\
20+`   x := y;`\
21+`   if (x < low(x)) or (x > high(x)) then`\
22+`   begin`\
23+`       runError(201);`\
24+`   end;`
3525
26+(The reported memory address at which the error occurred will differ and the compiler may produce optimized code.)
27+
3628### eligibility
3729
38-Range checks can be performed
30+Range checks can be performed
3931
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]].
32+- in [[Becomes|assignments]] (`:=`), and more specifically
33+ - when passing actual parameters, or
34+ - indicating [[Array|array]] indices, but also
35+- when doing [[Typecast|typecasts]].
4436
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.
37+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.
4638
4739### placement
4840
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.
41+The FPC allows enabling and disabling range checks on a per-statement basis, while Delphi allows changing the setting only a per-routine level.
5042
5143## caveats
5244
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.
45+- 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.
46+- If range checks are meant to be performed when passing parameters, range checks must be enabled *at* the call site.
5547
5648## application
5749
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:
59-```
60-{$push}
61- {$rangeChecks off}
62- x := y;
63- {$pop}
64-```
50+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:
6551
66-Use `{$ifOpt}` if code needs to be different on the current range-check-setting:
67-```
68-{$ifOpt R-} // or R+
69- …
70- {$endIf}
52+```delphi>
53+ {$push}
54+ {$rangeChecks off}
55+ x := y;
56+ {$pop}
57+</syntaxhighlight>
58+
59+Use <code>{$ifOpt}</code> if code needs to be different on the current range-check-setting:
60+<syntaxhighlight lang=
7161```
7262
63+`   {$ifOpt R-} // or R+`\
64+`       …`\
65+`   {$endIf}`
66+
7367---
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).*
68+*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).*
69+Category: FPC

Back to history