Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Signed long integer overflow detection in C

  1. Feb 22, 2012 #1
    Hi,
    I have three situations where might be overflow is occuring. I need to write test cases to resolve this problem. I don't know how to continue after this, please any one have suggestions to overcome this.

    Please help me.
     
  2. jcsd
  3. Feb 22, 2012 #2
    Post your code
     
  4. Feb 22, 2012 #3

    jedishrfu

    User Avatar

    Staff: Mentor

    http://www.phrack.org/issues.html?issue=60&id=10#article

    from the article it says that they cant be detected once they happen. So you need to develop an algorithm to detect it before it happens which can seriously slowdown your program but since its a testcase that may not matter.
     
  5. Feb 22, 2012 #4

    jedishrfu

    User Avatar

    Staff: Mentor

    i suppose you could do a second calculation in floating pt and then check to see if the integer answer is in the ballpark of the floating pt answer.
     
  6. Feb 22, 2012 #5

    rcgldr

    User Avatar
    Homework Helper

    Most processors have an overflow bit for integer math, but most compilers don't provide access to it (short of using inline assembly or assembly callable functions). Checking for overflow on addition can be done by checking to see if both addends have the same sign, and if so, if the sum also has the same sign (else overflow has occurred). Subtraction can be handled by negating the number to subtract and using the addition check. Overflow from multiplication may cause an exception depending on the cpu. If not, multiplication can be checked by dividing the product by one of the multiplicands to see if you get the other multiplicand. Overflow from division may cause an exception depending on the cpu. If not, you need to check the quotient by multiplying the quotient by the divisor to see if it matches the dividend.
     
  7. Feb 22, 2012 #6

    chiro

    User Avatar
    Science Advisor

    If you are using a language with exception catch an exception.

    If you are using something where you can add a kind of interrupt hook, then use that. Chances are if you are developing in an environment, then you will be able to do this.

    From this website:

     
  8. Feb 23, 2012 #7
    I agree with you if my case is regarded to unsigned integers. I am dealing with signed integers and the code was executed based on the user settings. Anyhow, i have read many documents that are explicitly saying that signed integer overflow is undefined(in C and C++) and creates lot of problems. I haven't experienced this before but it was really a nightmare for me. The code was written by some one else long ago. Recently, after including 20 lines code to the old one was creating the problem. I can not post that code here due some reasons. There is no syntax errors in that 20 lines but excution of that code was creating system crash. I can send it to some other mail if you want to have a look.
     
  9. Feb 23, 2012 #8
    if((current<0 && rStrom>0)||(current>0 && rStrom<0))
    {
    unerror=unrstrom-uncurrent;
    Error = (Signed long)unerror;
    unerrorint = unerrorint+unerror;
    ErrorInt = (Signed long)unerrorint;
    }
    else
    {
    Error=rStrom-current;
    ErrorInt=ErrorInt+Error;
    }


    I have not tested this yet but i have written the test case like above. All current,rstrom, error and errorint are declared as signed long at the beginning but i casted them to unsigned long to eliminate the overflow and then cast back to signed long. Any suggestions would be appreciated.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Signed long integer overflow detection in C
Loading...