473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

detecting arithmetic overflows - stdexcept help

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
3 2729
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BigMan | last post by:
Does the C++ standard say anything about how arithmetic exceptions (such as integer division by 0) interfere with C++ exceptions? I see that catch clauses DO catch arithmetic exceptions but...
4
by: christopher diggins | last post by:
Welcome to the first installment of the Diggins Public Domain Posts (PDP). There is a significant dearth of good public domain C++ code. All of it seems to come with one kind of a license or...
0
by: christopher diggins | last post by:
// binary_aritmetic.hpp // Diggins PDP (Public Domain Post) #1.2 // by Christopher Diggins, May 24, 2005 // // Comment: // Provides several algorithms for unsigned binary arithmetic // //...
10
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
10
by: vashwath | last post by:
Hi all, Is there any free tool available for detecting array overflow? I found one which detects overflow of dynamic arrays. But I need a tool(or a special compiler) which detects static array...
7
by: pocmatos | last post by:
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;
6
by: Andre Majorel | last post by:
How do you compute an off_t with overflow detection ? Ideally, the target language is C89/C90 and the target platform is reasonably recent versions of the major Unixen. If there is no practical...
9
by: Mike Aubury | last post by:
Is there any standard (or even non-standard) way to detect limit overflow in arithmetic in C ? eg. /* assuming 4 byte ints.. */ int a=2147483647; int b=2147483647; int c;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.