3 ms·
I don't see how it's wrong to use an if statement to check that nobody's passing in a zero to your function that later does division using that variable. c has
by jimbobthrowawy 2y ago
I don't see how it's wrong to use an if statement to check that nobody's passing in a zero to your function that later does division using that variable. c has a whole load of footguns in this area, as UB can impact code appearing earlier in the file. You are right about them being useless or counterproductive in effect sometimes though.
- pif 2y ago> I don't see how it's wrong to use an if statement to check that nobody's passing in a zero to your function that later does division using that variable. Indeed, it isn't. The check becomes useless if it happens after the division, though.
- nemetroid 2y agoOr if the check does not prevent the division (the typical "time-travel" example).
- kccqzy 2y agoDid you notice that the check of divisor being zero happens after the division, not before? It's undefined behavior because the behavior cannot be defined. On Intel you get a #DE before you do the check; this behavior cannot be standardized and that's why it's UB.