Thread: How can I check if a integer variable is overflowed?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    55

    How can I check if a integer variable is overflowed?

    Before the program crashes, that is.. I have no clue how to do this. I'm adding values to a variable and I want to check before it overflows.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The simplest is probably to compare your variable to INT_MAX, defined in limits.h.

    So, something like this: if( var < INT_MAX )

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    test if current value < last value

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( valuetoadd > maximumvalue - currentvalue )
        ...boo
    else
        ...yay
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    thanks for your answers, gentlemen. This will help me.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    test if current value < last value
    Odd as it may seem... this is probably the only reliable test you have... see if it rolled over the top.

    An integer will never be > INT_MAX ... as itCbitC points out it will roll back over to 0 or -1 first.

    Assuming 32 bits...
    Code:
    int a =  2147483645;
    a = a + 3;
    printf("%d",a);  // - 1
    
    unsigned int b = 4294967293;
    b = b + 3;
    printf("%d",b); // 1
    Last edited by CommonTater; 03-17-2011 at 11:13 PM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    Odd as it may seem... this is probably the only reliable test you have... see if it rolled over the top.

    An integer will never be > INT_MAX ... as itCbitC points out it will roll back over to 0 or -1 first.
    No but you can check for values below INT_MAX. My simple example assumes val++ though, otherwise you could do:

    Code:
    if( val + value_to_add < INT_MAX ) val += value_to_add;

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    Odd as it may seem... this is probably the only reliable test you have... see if it rolled over the top.
    For an unsigned integer type, this is true (they work with modulo arithmetic, so are required to "roll over the top".

    This is not true for signed integer types. That is why quzah's test (test before doing an operation) is needed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Subsonics View Post
    No but you can check for values below INT_MAX. My simple example assumes val++ though, otherwise you could do:

    Code:
    if( val + value_to_add < INT_MAX ) val += value_to_add;
    That won't help you because the test-add will itself overflow an integer add, producing a result less than INT_MAX.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    That won't help you because the test-add will itself overflow an integer add, producing a result less than INT_MAX.
    But then add this:

    Code:
    if( ((uint64_t)val + value_to_add) < INT_MAX ) val += value_to_add;

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Subsonics View Post
    But then add this:

    Code:
    if( ((uint64_t)val + value_to_add) < INT_MAX ) val += value_to_add;
    Which only works if val and value_to_add are 32 bits.

    Back in the good old Pascal days everyone was wondering how TP detected overflows at run time when in-code solutions like these failed so miserably. The answer turned out to be quite simple... The overflow flag in the CPU... a little bit of assembler at the end of each math function would check the OF flag, if it was lit up, the program tossed an exception.

    http://en.wikipedia.org/wiki/FLAGS_register_(computing)

    A similar process could probably be implemented in C, but it would most likely mean rewriting very large parts of everything to make it work...

    You might try...
    Code:
    if ((INT_MAX - current_value) > value_to_add)  
       current_value += value_to_add
    else
       DoTheMassivelyAnnoyingErrorThing();
    Last edited by CommonTater; 03-18-2011 at 09:29 AM.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    Which only works if val and value_to_add are 32 bits.

    Back in the good old Pascal days everyone was wondering how TP detected overflows at run time when in-code solutions like these failed so miserably. The answer turned out to be quite simple... The overflow flag in the CPU... a little bit of assembler at the end of each math function would check the OF flag, if it was lit up, the program tossed an exception.
    Of course but the question was about ints, otherwise you would not use INT_MAX, but really quazah's method works no matter what. And checking the overflow flag is a bit overkill I think, it's only set after the fact, so you would need to create a test function specific for addition and ints, something like this.

    Code:
    int overflow_add(int a, int b)
    {
        int test = a + b;
        int ofl = 0;
    
        __asm__(
            "seto %b0;"
            : "=g"(ofl)
            :
            : "%bh"
        );
    
        return ofl;
    }

    Quote Originally Posted by CommonTater View Post
    You might try...
    Code:
    if ((INT_MAX - current_value) > value_to_add)  
       current_value += value_to_add
    else
       DoTheMassivelyAnnoyingErrorThing();
    Obviously, it's already been posted and agreed upon.
    Last edited by Subsonics; 03-18-2011 at 10:05 AM.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by grumpy View Post
    For an unsigned integer type, this is true (they work with modulo arithmetic, so are required to "roll over the top".

    This is not true for signed integer types. That is why quzah's test (test before doing an operation) is needed.
    As is quzah's code won't work for signed and neither will current_value < last_value.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itCbitC View Post
    As is quzah's code won't work for signed and neither will current_value < last_value.
    Of course mine will, it will also work for self-imposed limits:
    Code:
    if( 300(add) > 1000(max) - 800(current) )
        boo
    else
        yay
    Unless you are talking about negative numbers, and going lower instead of going higher as you check for limits, but then all you do is change 'max' to 'min', and > to < and + to - ... which while easy enough to do, isn't what he asked for.

    Don't just tell me it doesn't work, show me how it doesn't work, because I'm not seeing it.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by quzah View Post
    Of course mine will, it will also work for self-imposed limits:
    Code:
    if( 300(add) > 1000(max) - 800(current) )
        boo
    else
        yay
    Unless you are talking about negative numbers, and going lower instead of going higher as you check for limits, but then all you do is change 'max' to 'min', and > to < and + to - ... which while easy enough to do, isn't what he asked for.

    Don't just tell me it doesn't work, show me how it doesn't work, because I'm not seeing it.


    Quzah.
    Yep! I meant negative numbers; we're talking about ints after all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  2. Check if variable is integer
    By Adoro in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2008, 09:26 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. Howto assign an address to an integer variable
    By daYz in forum C Programming
    Replies: 18
    Last Post: 02-24-2008, 10:19 AM