Signed long integer overflow detection in C

In summary, the author is trying to solve a problem where their code is causing a system crash. They are trying to find a way to prevent overflow from happening and they are looking for suggestions.
  • #1
rama1001
132
1
Hi,
I have three situations where might be overflow is occurring. I need to write test cases to resolve this problem. I don't know how to continue after this, please anyone have suggestions to overcome this.

Please help me.
 
Technology news on Phys.org
  • #2
Post your code
 
  • #4
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.
 
  • #5
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.
 
  • #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:

The Kernel's Trap Handler

The kernel's trap handler mediates handling of interrupts, exceptions, system service calls, and virtual memory management.

The difference between interrupts and exceptions is that interrupts occur asynchronously (for instance, when hardware peripheral devices needs processor attention), and exceptions occur as a part of standard application execution (for instance, when a math overflow occurs). So, exceptions are generally reproducable, but interrupts involve timing relationships that are difficult to reproduce.
 
  • #7
rcgldr said:
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.

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.
 
  • #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.
 

1. What is signed long integer overflow in C?

Signed long integer overflow in C is when the value of a signed long integer variable exceeds its maximum allowed value. This can happen when performing arithmetic operations on the variable, such as addition, subtraction, multiplication, or division.

2. Why is signed long integer overflow a concern?

Signed long integer overflow can lead to unexpected and incorrect results in a program. This can cause serious issues, especially in critical systems where accuracy is crucial. It is important to detect and handle signed long integer overflow to prevent such problems.

3. How does C detect signed long integer overflow?

C does not have built-in automatic detection for signed long integer overflow. It is the responsibility of the programmer to check for overflow and handle it appropriately. This can be done by using conditional statements or bitwise operations to compare the result of an operation with the maximum allowed value for a signed long integer.

4. What are some common methods for detecting signed long integer overflow in C?

One common method for detecting signed long integer overflow in C is to use the overflow flag in the processor's status register. This flag is set when an arithmetic operation results in overflow. Another method is to use built-in functions, such as labs() or llabs(), to check if the result of an operation is larger than the maximum allowed value for a signed long integer.

5. How can we handle signed long integer overflow in C?

There are several ways to handle signed long integer overflow in C. One approach is to use conditional statements to check for overflow before performing an operation and take appropriate action, such as displaying an error message or adjusting the value. Another approach is to use data types with larger range, such as unsigned long long, to store the result of an operation. Additionally, there are libraries available that provide functions for detecting and handling signed long integer overflow in C.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
Replies
9
Views
944
  • Programming and Computer Science
Replies
17
Views
994
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Materials and Chemical Engineering
Replies
24
Views
2K
  • Programming and Computer Science
Replies
21
Views
3K
  • Precalculus Mathematics Homework Help
Replies
5
Views
754
Replies
6
Views
785
Back
Top