0

Edit (IMPORTANT): This question was more specific than the question this was marked as a duplicate against. This was asking how to do it with a boolean function. But now I know that it just doesn't work like that. This question shows that a + b = c can only be overflow checked if you write if(c - b == a) and that there is no operator independent way of checking.

Once the overflow happened, you cannot detect it

-lorro

I have been looking for a way to detect integer overflow in C++ using an if statement.

Possible pseudo code:

#include <iostream>
#include <...>

using namespace std;

bool isOverflow(...);

int main()
    {
    int a = INT_MAX + 1;

    if (isOverflow(...))
    {
        cout << "Overflow" << endl;
    }
    else
    {
        cout << "No Overflow" << endl;
    }
    return 0;
}

bool isOverflow
{
    ...
}

OK to be honest this pseudo code preference may not work, but i've seen this question asked many times and have not found any useful answers. It may require unsigned or unsigned long long, although I'm not necessarily encouraging the use of those.

EDIT:

I would like to use it with a multiplication sentence with 3 numbers:

a * a * b

I am aware that there is a pow function in <math.h> but that's off topic.

I am also aware that if I want an accurate int result from pow I would use:

int(pow(base, index) + 0.5)
FluorescentGreen5
  • 879
  • 11
  • 23
  • 1
    http://en.cppreference.com/w/cpp/types/numeric_limits Once the overflow happened, you cannot detect it - what you can do is have a temp var with more precision to calculate the result, then cast it back iff it's within numeric_limits. – lorro Jun 30 '16 at 07:17
  • There are so many answers for this question. Here is an answer using signed integers: http://stackoverflow.com/a/1514309/4082723 – 2501 Jun 30 '16 at 07:22

1 Answers1

0

It depends on what sort of operation you are using and what are the types of the operands.

e.g. if you want to detect an overflow after addition, and both operands are unsigned integers, then an overflow will have occurred if the result is less than the sum of both operands.

bool overflow;
if (a+b < a)
   overflow = true;
else
   overflow = false;

For signed integers, you can refer to the excellent post here

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • I would like to use it on a 3 numbered multiplication sentence: `a * a * b`. I was getting suspicious when I saw a down vote on your answer. – FluorescentGreen5 Jun 30 '16 at 07:53
  • there are some CPUs with an overflow flag, but it seems like that flag can't be accessed by C++. I guess the best way to do it is to do the calculation in reverse and see if I get the same starting number. – FluorescentGreen5 Sep 22 '16 at 11:17
  • 1
    Another option for multiplying two `uint32_t` 's is to have the result in uint64_t and check if the result is less than 2^32. For 3 multiplications, you should first use the procedure for 2 multiplications and then the same procedure on the result and the 3rd value. – Rishikesh Raje Sep 22 '16 at 11:24