0
class Divide
{
public:
    float divident, divisor;
    Divide():divident(10.0f),divisor(0.0f){}
};

int main()
{
    Divide obj[100];
    int quotient = obj[1].divident/obj[1].divisor;
    return quotient;
}

Edit: Compiler Qt 5.3.1 , Windows 7-32 bit.

Why is there no division by zero warning at compile time or a run time crash happening?

b4hand
  • 9,550
  • 4
  • 44
  • 49
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68

2 Answers2

6

It doesn't crash because you've got a floating-point division by zero, not an integer division by zero. Floating-point division by zero is a valid way to obtain infinity.

The conversion from float to int is undefined, since infinity is not in int's range, so crashing would be allowed there, but that is simply not what typical implementations make it do.

0

That's quite a code analysis you would be expecting on the part of the compiler in this particular case. I can't think of an existing compiler that would give you that level of analysis. I don't have much more of an answer than that.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74