gdb中的算術異常,但是我沒有除以0 ?

[英]Arithmetic Exception in gdb, but I'm not dividing by zero?


I've been getting a Floating point exception (core dumped) error in my C++ program, and gdb shows that the problem is on a line that performs modulo division:

我在c++程序中遇到了浮點異常(核心轉儲)錯誤,gdb顯示問題在執行模塊划分的行上:

Program received signal SIGFPE, Arithmetic exception.
[Switching to Thread 0x7ffff6804700 (LWP 13931)]
0x00000000004023e8 in CompExp::eval (this=0x7fffec000e40, currVal=0)
    at exp.cpp:55
55              return (r==0) ? 0 : l % r;

The line guards against dividing by zero, and my backtrace shows the following:

線衛反對除以零,我的回溯顯示如下:

#0  0x00000000004023e8 in CompExp::eval (this=0x7fffec000e40, currVal=0)
    at exp.cpp:55
        l = -2147483648
        r = -1

Since I know I'm not dividing by zero, what else could possibly be causing the exception?

既然我知道我沒有除以0,還有什么可能導致這個異常呢?

3 个解决方案

#1


3  

So I figured out what was causing the problem -- An arithmetic exception can be triggered either by dividing by zero, or overflow of a signed integer, which is what happened here. Unsigned integers are required to wrap around when overflowed; the behavior for signed integers is undefined.

我知道是什么導致了這個問題,一個算術異常可以通過除以0觸發,或者一個有符號整數的溢出,這就是這里發生的。無符號整數在溢出時需要打包;符號整數的行為沒有定義。

#2


2  

Change the code to the following to avoid trying to take the modulo of a negative number which is undefined:

將代碼更改為以下代碼,以避免采用未定義的負數的模量:

return (r<=0) ? 0 : l % r;

#3


1  

In order to calculate such modulo expression: -2147483648 % -1, a division is required, which in this case, seems to be a 32-bit division (I guess l and r are defined as int). The right result of such division would be 2147483648, but that value cannot be represented in 32-bit, so an arithmetic exception is produced.

為了計算這樣的模式:- 2147483648% % -1,需要一個除法,在本例中,這個除法似乎是一個32位除法(我猜l和r被定義為int)。這種划分的正確結果將是2147483648,但是這個值不能用32位表示,因此會產生一個算術異常。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/12/31/729b4534294d69c0cdc35a34b6177b7f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com