How do division by zero exceptions work?

How is division calculated at the compiler / chip level?

And why does C ++ always throw these exceptions at runtime instead of compilation time (in the case where the divisor is known as zero at compile time)?

+4
source share
4 answers

It is entirely up to the compiler. You can if you want to write an extension for your compiler to check this problem.

For example, visual C ++:

+2
source
  • It depends. Some processors have hardware sharing instructions. Some processors must do the calculation, this software.
  • Some C ++ compilers are also not caught at runtime. Often due to the lack of hardware support for the trap when dividing by zero.
+2
source
  • At the chip level, separation, of course, is done using circuits. Here is an overview of the binary division scheme.
  • Because the C ++ compiler just does not check for divisors that are guaranteed to be 0. It could check this.
+1
source
  • Large search tables. Remember these multiplication tables from school? Same idea, but division instead of multiplication. Obviously, not every number is there, but the number is broken into pieces, and then narrowed through the table.

  • Separation occurs at runtime, not at compile time. Yes, the compiler could see that the divisor is zero, but most people should not write an invalid statement like this.

0
source

All Articles