| New Reply |
signed long integer overflow detection in C |
Share Thread |
| Feb22-12, 07:26 AM | #1 |
|
|
signed long integer overflow detection in C
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. |
| Feb22-12, 09:59 AM | #2 |
|
|
Post your code
|
| Feb22-12, 10:07 AM | #3 |
|
|
http://www.phrack.org/issues.html?is...&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. |
| Feb22-12, 10:11 AM | #4 |
|
|
signed long integer overflow detection in C
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.
|
| Feb22-12, 11:54 AM | #5 |
|
Recognitions:
|
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.
|
| Feb22-12, 09:54 PM | #6 |
|
|
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: |
| Feb23-12, 03:20 AM | #7 |
|
|
|
| Feb23-12, 04:41 AM | #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. |
| New Reply |
Similar discussions for: signed long integer overflow detection in C
|
||||
| Thread | Forum | Replies | ||
| Angular momentum - integer or half-integer | Quantum Physics | 2 | ||
| Log Overflow Problem | Precalculus Mathematics Homework | 2 | ||
| Proof Question: Prove integer + 1/2 is not an integer | Calculus & Beyond Homework | 4 | ||
| Stack Overflow with Quicksort | Programming & Comp Sci | 7 | ||
| Arithmetic Overflow | Engineering, Comp Sci, & Technology Homework | 3 | ||