Thread: Checking overflow?

  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,897
    >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, 09:38 AM
  2. reliably checking for overflow before addition?
    By cyberfish in forum C++ Programming
    Replies: 18
    Last Post: 06-23-2008, 03:35 AM
  3. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02: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, 09:05 AM