Delphi:如何使用 $OVERFLOWCHECKS OFF 禁用溢出检查?
我有一些代码会导致下溢:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
delta := (t2 - t1);
减法本身确实会生成溢出(下溢),但我不希望 Delphi 抛出 EIntOverflow
异常。因此,我尝试通过禁用溢出检查来禁用溢出检查代码的生成:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
{$OVERFLOWCHECKS OFF}
delta := (t2 - t1);
{$OVERFLOWCHECKS ON}
但即使使用 OVERFLOWCHECKS OFF 选项,它仍然会引发异常。生成的代码仍然包含检查:
关于 $Q
的文档提醒:
溢出检查
类型开关
语法 {$Q+} 或 {$Q-}
{$OVERFLOWCHECKS 开启} 或 {$OVERFLOWCHECKS 关闭}
默认 {$Q-}
{$OVERFLOWCHECKS 关闭}
范围本地备注
$Q 指令控制 生成溢出检查代码。 在{$Q+}状态下,某个整数 算术运算(+、-、*、Abs、 Sqr、Succ、Pred、Inc 和 Dec)是 检查是否溢出。代码为 这些整数算术中的每一个 操作之后是附加的 验证结果的代码 在支持的范围内。如果一个 溢出检查失败,EIntOverflow 引发异常(或者程序是 如果异常处理被终止 未启用)。
$Q 开关通常用于 与 $R 开关结合使用, 启用和禁用生成 范围检查代码。启用溢出 检查会减慢你的程序并且 让它变得更大一些,所以使用 {$Q+} 仅用于调试。
如何使用 $OVERFLOWCHECKS OFF
禁用溢出检查代码的生成?
梅森的回答奏效了。修改后的代码是:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
delta := Subtract(t2, t1);
{$OVERFLOWCHECKS OFF}
function Subtract(const B, A: DWORD): DWORD; //subtract B-A
begin
{
Disabling overflow checking does not work at the line level,
only the routine level.
Hence the function to subtract two numbers.
}
Result := (B-A);
end;
{$OVERFLOWCHECKS ON}
对于谷歌爬虫,替代问题措辞:如何暂时禁用Delphi中的溢出检查?
i have bit of code that causes an underflow:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
delta := (t2 - t1);
The subtraction itself does generate an overflow (underflow), but i don't want Delphi to throw an EIntOverflow
exception. So i try disabling the generation of overflow checking code by disabling overflow checking:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
{$OVERFLOWCHECKS OFF}
delta := (t2 - t1);
{$OVERFLOWCHECKS ON}
Yet even with the OVERFLOWCHECKS OFF
option, it still throws an exception. And the generated code still contains the check:
A reminder of the documentation on $Q
:
Overflow checking
Type Switch
Syntax {$Q+} or {$Q-}
{$OVERFLOWCHECKS ON} or {$OVERFLOWCHECKS OFF}
Default {$Q-}
{$OVERFLOWCHECKS OFF}
Scope LocalRemarks
The $Q directive controls the
generation of overflow checking code.
In the {$Q+} state, certain integer
arithmetic operations (+, -, *, Abs,
Sqr, Succ, Pred, Inc, and Dec) are
checked for overflow. The code for
each of these integer arithmetic
operations is followed by additional
code that verifies that the result is
within the supported range. If an
overflow check fails, an EIntOverflow
exception is raised (or the program is
terminated if exception handling is
not enabled).The $Q switch is usually used in
conjunction with the $R switch, which
enables and disables the generation of
range-checking code. Enabling overflow
checking slows down your program and
makes it somewhat larger, so use {$Q+}
only for debugging.
How do i use $OVERFLOWCHECKS OFF
to disable the generation of overflow checking code?
Mason's answer worked. The revised code is:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
delta := Subtract(t2, t1);
{$OVERFLOWCHECKS OFF}
function Subtract(const B, A: DWORD): DWORD; //subtract B-A
begin
{
Disabling overflow checking does not work at the line level,
only the routine level.
Hence the function to subtract two numbers.
}
Result := (B-A);
end;
{$OVERFLOWCHECKS ON}
For google crawler, alternate question phrasing: How to temporarily disable overflow checking in Delphi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在线路级别不起作用。您需要将其关闭才能完成整个功能。
It doesn't work at the line level. You need to turn it off for the entire function.