Checking overflow?

This is a discussion on Checking overflow? within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how to check for overflow in a c program? like when: Code: result=var1+var2; How do I know ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Checking overflow?

    Does anyone know how to check for overflow in a c program?

    like when:

    Code:
    result=var1+var2;
    How do I know if an overflow has occured?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do I know if an overflow has occured?
    You cannot do so portably. If overflow is likely then using a larger data type would be a safe alternative. You can also test each value before the operation to determine if overflow is possible.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. reliably checking for overflow before addition?
    By cyberfish in forum C++ Programming
    Replies: 18
    Last Post: 06-23-2008, 04:35 AM
  3. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 03:09 PM
  4. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21