detecting arithmetic overflows - stdexcept help

K

Karthik

Hi,
I am writing this application that needs a lot of arithmetic
calculations.
I was wondering if C++ language specifies any way of detecting
arithmetic overflows.
Let us consider the following program.

#include <iostream>

using namespace std;

int main() {
short a = -10000;
short b = a * 8;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
}

My implementation says it to be -

$ ./a.out
a = -10000
b = -14464


How would I find if an overflow had occured ?

I was looking at overflow_error class provided by stdexcept library in
C++. That does not seem to be raised automatically.


#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
try {
short a = -10000;
short b = a * 8;
} catch ( overflow_error & r ) {
// No. arithmetic exception is not caught here.
}
}

This being the case how would i catch arithmetic overflows ?
 
V

Victor Bazarov

Karthik said:
I am writing this application that needs a lot of arithmetic
calculations.
I was wondering if C++ language specifies any way of detecting
arithmetic overflows.

Nope. There could a way in your compiler-specific library or as
an extension.

It is usually better to prevent overflow than to detect it post factum.
Let us consider the following program.

#include <iostream>

using namespace std;

int main() {
short a = -10000;
short b = a * 8;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
}

My implementation says it to be -

$ ./a.out
a = -10000
b = -14464


How would I find if an overflow had occured ?

There is no way in C++ as far as I know.
I was looking at overflow_error class provided by stdexcept library in
C++. That does not seem to be raised automatically.

std::eek:verflow_error is thrown by the bitset's to_ulong function if the
integral value that corresponds to the bits cannot be represented in
an unsigned long. You're free to throw it yourself if you encounter
a situation that calls for it.
#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
try {
short a = -10000;
short b = a * 8;
} catch ( overflow_error & r ) {
// No. arithmetic exception is not caught here.
}
}

This being the case how would i catch arithmetic overflows ?

No compiler-independent way. What happens when a signed integer overflows
is implementation-defined. FWIW, it could just wrap around without any
exceptional situation.

Victor
 
P

Pete Becker

Victor said:
What happens when a signed integer overflows
is implementation-defined.

Actually, it's undefined. Except when it occurs in a constant expression
-- in that case the program is ill-formed.
 
K

Karthik

Pete said:
Actually, it's undefined. Except when it occurs in a constant expression
-- in that case the program is ill-formed.

Thanks Victor, Pete for your comments. I guess I would route this
question to the gcc mailing list.

- Karthik.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top