P
pocmatos
Hi all,
What the best way to detect under/over flow in a program with a lot of
computations?
For example:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int max = std::numeric_limits<int>::max();
int overflow = max + 1;
cout << "initial: " << max << endl << "overflow: " << overflow <<
endl;
return 0;
}
This silently outputs (even by compiling with -Wall using g++):
$ ./overflow
initial: 2147483647
overflow: -2147483648
The nice thing would be to have something like an exception thrown when
this happens since after an overflow, a program which makes use of a
lot of computations is useless after that.
Any ideas on how to detecting these and underflows? (I'm interested
also in non-portable solutions if you know about any)
Thanks,
Paulo Matos
What the best way to detect under/over flow in a program with a lot of
computations?
For example:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int max = std::numeric_limits<int>::max();
int overflow = max + 1;
cout << "initial: " << max << endl << "overflow: " << overflow <<
endl;
return 0;
}
This silently outputs (even by compiling with -Wall using g++):
$ ./overflow
initial: 2147483647
overflow: -2147483648
The nice thing would be to have something like an exception thrown when
this happens since after an overflow, a program which makes use of a
lot of computations is useless after that.
Any ideas on how to detecting these and underflows? (I'm interested
also in non-portable solutions if you know about any)
Thanks,
Paulo Matos