5

I just wonder if there is some convenient way to detect if overflow happens to any variable of any default data type used in a C++ program during runtime? By convenient, I mean no need to write code to follow each variable if it is in the range of its data type every time its value changes. Or if it is impossible to achieve this, how would you do?

For example,

float f1=FLT_MAX+1;
cout << f1 << endl;

doesn't give any error or warning in either compilation with "gcc -W -Wall" or running.

Thanks and regards!

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
Tim
  • 1
  • 141
  • 372
  • 590
  • I would speculate that you could possible set up an interrupt handler to do it? Assuming that overflow actually generates an interrupt. As you can tell, I'm merely speculating here, hence adding it as a comment rather than an answer. – korona Sep 22 '09 at 14:26
  • 3
    is it not better to write code that prevents (or makes them harder to happen) overflows rather than trying to catch it happening in the act? I am just throwing the thought out. – Jay Sep 22 '09 at 14:29
  • @Jay: not necessarily. Especially in floating-point, there are some classes of algorithms for which overflow is very, very rare, and the added computational expense of preventing it entirely is very large. In a performance critical situation, you might rather use the fast algorithm that doesn't prevent overflow, and replay the one calculation in a million for which overflow occurs using the careful, slower algorithm that prevents overflow. – Stephen Canon Sep 22 '09 at 14:58
  • @stephentyrone ... I sit corrected, thanks. :-) – Jay Sep 22 '09 at 15:26
  • 1
    If you're really lucky, your IEEE float implementation lets you set the overflow mode, so you can configure at runtime whether overflows clamp at infinity, or give a hardware exception. For example, http://www.gnu.org/software/gsl/manual/html_node/Setting-up-your-IEEE-environment.html – Steve Jessop Sep 22 '09 at 16:13
  • Does this answer your question? [How do I detect unsigned integer multiply overflow?](https://stackoverflow.com/questions/199333/how-do-i-detect-unsigned-integer-multiply-overflow) – miken32 Dec 03 '19 at 01:45

5 Answers5

6

Consider using boosts numeric conversion which gives you negative_overflow and positive_overflow exceptions (examples).

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • +1. not only does this seem like a good solution, but I learned that "negative overflow" and "underflow" are not the same thing... silly me. – rmeador Sep 22 '09 at 14:34
4

Your example doesn't actually overflow in the default floating-point environment in a IEEE-754 compliant system.

On such a system, where float is 32 bit binary floating point, FLT_MAX is 0x1.fffffep127 in C99 hexadecimal floating point notation. Writing it out as an integer in hex, it looks like this:

0xffffff00000000000000000000000000

Adding one (without rounding, as though the values were arbitrary precision integers), gives:

0xffffff00000000000000000000000001

But in the default floating-point environment on an IEEE-754 compliant system, any value between

0xfffffe80000000000000000000000000

and

0xffffff80000000000000000000000000

(which includes the value you have specified) is rounded to FLT_MAX. No overflow occurs.

Compounding the matter, your expression (FLT_MAX + 1) is likely to be evaluated at compile time, not runtime, since it has no side effects visible to your program.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
3

In situations where I need to detect overflow, I use SafeInt<T>. It's a cross platform solution which throws an exception in overflow situations.

SafeInt<float> f1 = FLT_MAX;
f1 += 1; // throws

It is available on codeplex

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

Back in the old days when I was developing C++ (199x) we used a tool called Purify. Back then it was a tool that instrumented the object code and logged everything 'bad' during a test run. I did a quick google and I'm not quite sure if it still exists.

As far as I know nowadays several open source tools exist that do more or less the same. Checkout electricfence and valgrind.

Niels Basjes
  • 10,424
  • 9
  • 50
  • 66
0

Clang provides -fsanitize=signed-integer-overflow and -fsanitize=unsigned-integer-overflow.

http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196