Thread: detect variable overflow

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    38

    detect variable overflow

    is there a provision by which i can detect overflow in a variable during arithmatic operation
    for example i use an unsigned char variable a to store the sum of b and c both unsigned chars. I want to detect if b+c is more that 255 (i.e more than what 8-bit number can store).

    please help

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here are a couple of ways.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void foo(unsigned char b, unsigned char c)
    {
       unsigned char a = b + c;
       if ( a < c )
       {
          printf("overflow occurred, ");
       }
       printf("a = %d\n", a);
    }
    
    void bar(unsigned char b, unsigned char c)
    {
       unsigned char a = b + c;
       if ( b > UCHAR_MAX - c )
       {
          printf("overflow occurred, ");
       }
       printf("a = %d\n", a);
    }
    
    int main(void)
    {
       foo(10, 10);
       foo(UCHAR_MAX - 5, 10);
       foo(10, UCHAR_MAX - 5);
       foo(UCHAR_MAX - 5, UCHAR_MAX - 5);
       bar(10, 10);
       bar(UCHAR_MAX - 5, 10);
       bar(10, UCHAR_MAX - 5);
       bar(UCHAR_MAX - 5, UCHAR_MAX - 5);
       return 0;
    }
    
    /* my output
    a = 20
    overflow occurred, a = 4
    overflow occurred, a = 4
    overflow occurred, a = 244
    a = 20
    overflow occurred, a = 4
    overflow occurred, a = 4
    overflow occurred, a = 244
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int overflow( unsigned char a, unsigned char b )
    {
            return UCHAR_MAX - a < b; /*[edit]Good point Dave.[/edit]*/
            /*return  a > b ? UCHAR_MAX - a < b : UCHAR_MAX - b < a;*/
    }
    Something like that will suffice. You can do the same with other data types using the same concept. This will actually avoid the overflow, rather than relying on the overflow itself to actually happen to see if it comes out the way you expect.

    [edit]For some reason I was thinking the order mattered. Thank's Dave. I need a nap.[/edit]

    Quzah.
    Last edited by quzah; 05-12-2004 at 01:41 PM. Reason: Good point dave.
    Hope is the first step on the road to disappointment.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    ..Beaten..

    >> for example i use an unsigned char variable a to store the sum of b and c both unsigned chars ... I want to detect if b+c is more that 255 <<

    If a and b are both unsigned characters you can do it in the normal way:
    Code:
    if (a + b > 255)
    This is because C promotes types smaller than int to int before performing arithmetic operations on them. So in the above expression a and b are treated as ints and no overflow will occur.

    You can get into trouble when you wish to add two ints together as these can overflow.

    You must add an extra overflow check in this scenario.
    Code:
    if (A + B >= A && A + B < MAX) 
    {
       // No overflow and less than MAX
    }
    
    // Or, the other way around.
    if (A + B < A)
    {
       printf("OVERFLOW: Stop trying to hack me!");
    }
    Here is a good article on integer overflows.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    thanx quzah ....thanx anonytmouse
    but my problem still remains unsolved
    i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
    and why i am looking for that is because of following:
    my problem is not as simple as adding just 2 unsigned chars ..........suppose i have to add three unsigned chars a,b,c i used "if(a+b+c<a)" (similar to used in the foo function) before posting my query on the board now consider a=23 b=255 c=1 so now a+b+c=a therefore overflow not detected
    i can handle this special case (actally i have done) as an excetional case but was looking for a better solution
    any suggestions???

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    127
    >i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
    The spirit of C is that you as the programmer know exactly what you're doing. The language definition will not protect you from yourself in cases such as these, so you must do it manually.

    >any suggestions???
    Write a function similar to quzah's with a variable number of arguments that tests and calculates intermediate results incrementally. After "running the gauntlet", you can return the result, or flag an error if any of the tests fail.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    Quote Originally Posted by Kip Neuhart
    >i was looking for something inbuilt in c .....some provision given by c..like some flag that is set or something like this
    The spirit of C is that you as the programmer know exactly what you're doing.
    i respect that and i am the beliver of doing things myself
    as i told u i have sucessfully handled the situation so that's not an issue
    all i wanted to know that is there some thing in c like the carry flags in microprocessors that may be set if there is an overflow in some variable
    that's it

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    127
    all i wanted to know that is there some thing in c like the carry flags in microprocessors that may be set if there is an overflow in some variable
    that's it
    And I gave you your answer. No, there is not.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    I'm very sorry for not crediting(by mistake) the greatest contributer DAVE_sinkula

    thanx dave
    though my question is still unaswered

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    Quote Originally Posted by Kip Neuhart
    And I gave you your answer. No, there is not.
    Okay Sir. Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. Using variable overflow for random numbers - wise?
    By ulillillia in forum C Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM