范围检查错误的原因(Delphi) [英] Cause of Range Check Error (Delphi)

查看:804
本文介绍了范围检查错误的原因(Delphi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个压缩版本的一些代码,导致范围检查错误和溢出错误,我应该打开这些编译器检查指令。我明白为什么会导致溢出,在C1的乘法上,似乎可能会超过数据类型的最大值。但是为什么会触发范围检查错误? Delphi的文档和堆栈溢出的其他帖子使其听起来像范围检查错误通常是超出范围的数组访问。但是我没有访问一个数组,它的意思是导致范围检查错误。也许它对param1的任务?但是为什么会是范围检查,而不是溢出错误,如果是这样的话?

Here's a condensed version of some code that causes both a Range check error and an overflow error, should I turn on those compiler check directives. I understand why this would cause an overflow, on the multiplication of C1, it seems likely it might exceed the data-type's max valude. But why would this also trigger a Range-check error? Delphi's documentation and other posts on stack overflow make it sound like range-check errors are usually for array accesses that are out of bounds. But I'm not accessing an array on the line it's saying is causing the range-check error. Perhaps its on the assignment to param1? But why would that be a range-check and not an overflow error, if so?

const
  C1 = 44001;
  C2 = 17999;

function fxnName(..other params...; param1: Word): String;
var
  someByte: byte;
begin
  // some code
  // by now we're in a loop. the following line is where it breaks to in the debugger: 
  param1 := (someByte + param1) * C1 + C2;
  // more code
end;

如果它是相关的,当它在调试器中的那一行断开时,所有的值看起来都是预期的,除了param1,当我要求Delphi评估它时,显示Undeclared identifier:'param1'。

If it's relevant, when it breaks on that line in the debugger, all the values look as expected, except param1, which shows "Undeclared identifier: 'param1'" when I ask Delphi to evaluate it.

推荐答案

有关范围检查的文档说明:

The documents about range-checking states:


$ R指令启用或禁用生成范围检查代码。在{$ R +}状态下,所有数组和字符串索引表达式都被验证为在定义的边界内,并且标量和子范围变量的所有分配都被检查在范围内。如果范围检查失败,则会引发ERangeError异常(如果未启用异常处理,程序将终止)。

The $R directive enables or disables the generation of range-checking code. In the {$R+} state, all array and string-indexing expressions are verified as being within the defined bounds, and all assignments to scalar and subrange variables are checked to be within range. If a range check fails, an ERangeError exception is raised (or the program is terminated if exception handling is not enabled).

所以这里的原因是赋值给一个标量值,它被传递一个已经通过上限的值。

So the reason here is the assignment to a scalar value, which is handed a value that has passed the upper range.

另请参见 docwiki简单类型

示例:

{$R+} // Range check on
var
  w1,w2 : word;
begin
  w1 := High(word);
  w1 := w1 + 10; // causes range-check error on assignment to w1 (upper range passed)
  w2 := 0;
  w2 := w2 - 10; // causes range-check error on assignment to w2 (lower range passed)
end;

所有平台无关整数类型的$ R和$ Q的所有组合的汇总测试: / p>

A summary test of all combinations of $R and $Q for all platform-independent integer types:

            R+Q+  R+Q-  R-Q+
 ShortInt    R     R     x
 SmallInt    R     R     x
 Integer     O     x     O
 LongInt     O     x     O
 Int64       O     x     O
 Byte        R     R     x
 Word        R     R     x
 LongWord    O     x     O
 Cardinal    O     x     O
 UInt64      O     x     O

R =范围错误; O =溢出错误; x = Nothing

R=Range error; O=Overflow error; x=Nothing

而测试是32位模式下的XE2(伪代码):

And the test was(pseudo-code) with XE2 in 32-bit mode:

number := High(TNumber);
number := number + 1;

这篇关于范围检查错误的原因(Delphi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆