By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

detecting arithmetic overflows - stdexcept help

P: n/a
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 ?

--
Karthik.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Karthik wrote:
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::overflow_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
Jul 22 '05 #2

P: n/a
Victor Bazarov wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3

P: n/a
Pete Becker wrote:
Victor Bazarov wrote:
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.


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

- Karthik.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.