Delphi:如何使用 $OVERFLOWCHECKS OFF 禁用溢出检查?

标签 delphi delphi-5 integer-overflow

我有一些代码会导致下溢:

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选项,它仍然会抛出异常。并且生成的代码仍然包含检查:

alt text

关于 $Q 的文档提醒:

Overflow checking

Type Switch
Syntax {$Q+} or {$Q-}
{$OVERFLOWCHECKS ON} or {$OVERFLOWCHECKS OFF}
Default {$Q-}
{$OVERFLOWCHECKS OFF}
Scope Local

Remarks

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.

如何使用 $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 中的溢出检查?

最佳答案

它在线路级别不起作用。您需要将其关闭才能使用整个功能。

关于Delphi:如何使用 $OVERFLOWCHECKS OFF 禁用溢出检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2418272/

相关文章:

delphi - 保持打开的连接 TCP/IP

delphi - Deplhi 5 中的编译器指令 SETPEFlags

java - Integer.MIN_VALUE 除以 -1

Delphi:在运行时查找从给定基类派生的类?

delphi - TWebBrowser 因嵌入 Youtube 剪辑而崩溃

generics - 限制可包装使用的模板

go - 如何在 Go 中将常量设置为负值

Delphi - E2010 不兼容类型 : 'Integer' and 'Char' - Any ideas

delphi - 有没有办法在多选项卡中组织 Delphi XE2/XE5 选项卡编辑器?

Delphi 7 : Converting (serializing? ) TByteDynArray 的对象(用于 SOAP)