2 ms·
> It’s very trivial to check if x * y would overflow though and you should just learn the extremely simple and obvious test. if (y && x > (T)-1/y) I think
by pornel 5y ago
> It’s very trivial to check if x * y would overflow though and you should just learn the extremely simple and obvious test.
if (y && x > (T)-1/y)
I think it's sarcasm, but with C guides like this it's never certain :)
- ufo 5y agoMaybe this is not a problem for calculating the size of a memory allocation, but that division operation is going to be several times slower than the multiplication it is guarding. Is there a faster way to check for overflow, perhaps using compiler builtins?
- saberience 5y agoMaybe I'm just a moron but what makes this test "obvious"? What is T?
- afiori 5y agoi imagine it is the type of x and y