gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:57:21 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com