简体   繁体   English

C ++和GCC:GCC的C ++实现如何处理除零?

[英]C++ & GCC: How Does GCC's C++ Implementation Handle Division by Zero?

Just out of interest. 只是出于兴趣。 How does GCC's C++ implementation handle it's standard number types being divided by zero? GCC的C ++实现如何处理标准数字类型除以零? Also interested in hearing about how other compiler's work in relation to zero division. 也有兴趣了解其他编译器如何在零分割方面工作。 Feel free to go into detail. 随意详细说明。 This is not purely for entertainment as it semi-relates to a uni assignment. 这不仅仅是为了娱乐,因为它与单一的任务半关联。

Cheers, Chaz 干杯,Chaz

It doesn't. 它没有。 What usually happens is that the CPU will throw an internal exception of some sort when a divide instruction has a 0 for the operand, which will trigger an interrupt handler that reads the status of the various registers on a CPU and handles it, usually by converting it into signal that is sent back to the program and handled by any registered signal handlers. 通常发生的情况是,当除法指令的操作数为0时,CPU将抛出某种类型的内部异常,这将触发一个中断处理程序,该处理程序读取CPU上各种寄存器的状态并处理它,通常是通过转换它被发送回发送给程序并由任何注册信号处理程序处理的信号。 In the case of most unix like OSes, they get a SIGFPE. 对于像操作系统这样的大多数unix,它们都有一个SIGFPE。

While the behavior can vary (for instance on some CPUs you can tell the CPU not to raise an exception, generally they just put some clamped value in like 0 or MAXINT), that variation is generally due to differences in the OS, CPUm and runtime environment, not the compiler. 虽然行为可能会有所不同(例如,在某些CPU上,您可以告诉CPU不要引发异常,通常它们只是将一些钳位值设置为0或MAXINT),这种变化通常是由于OS,CPUm和运行时的差异造成的。环境,而不是编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM