Thread: Overflow check

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Overflow check

    Hi,
    Is there any way to check if there has been an overflow after an assignment?
    Like checking the Status register or something.
    Thanks..

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What kind of assignment? Regular old x=3? [edit] If so you could compare the value casted to the smaller type with the value.
    Code:
    if(10000000000L != (short)10000000000L) overflow();
    [/edit]

    Are you adding something to a variable, x+=3? If so you could try something like this:
    Code:
    #include <limits.h>
    
    short x = SHRT_MAX;
    
    if(1 + (long)x > SHRT_MAX) overflow();
    x += 1;
    If there is such a thing as a "Status register" it isn't part of the ANSI standard.

    [edit=2] This is integer overflow, right? http://en.wikipedia.org/wiki/Integer_overflow [/edit]
    Last edited by dwks; 12-23-2006 at 11:53 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It can be done without casting
    Code:
    int x = 6;
    if(x> INT_MAX - 3) overflow();
    else x += 3;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I knew that . . .

    But all the examples you've been given check for overflow before you assign the value. If you've already gone ahead and assigned the value, there's no way to tell if the integer overflowed (unless you have access to the original value, in which case you could check for overflow before the assignment anyway).

    The constants in limits.h are very useful for this kind of thing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Normally, a flag is set in a status register by the processor when an assignment gives an overflow. That's what i 'm telling. I don't want to manually check for overflow.
    The question is if i can check it..

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, that is definitely non-standard. Registers are non-standard. How about stating basic information, like compiler (Turbo C?) and OS (WinXP?) and processor (Intel 32 bit?).

    You might try googling, perhaps for "c programming status register integer overflow". The first hit is
    http://www.osdata.com/topic/language/asm/register.htm
    [edit] From that page:
    overflow Set if arithmetic overflow occurs. Used in Digital VAX [V], Intel 80x86 [OF], Motorola 680x0 [V], Motorola 68300 [V], Motorola M68HC16 [V].
    As you can see, it's very processor-dependant.

    It would be better to use one of the standard C methods outlined above if possible. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Floating Exception (Core Dumped)
    By DarrenY in forum C Programming
    Replies: 9
    Last Post: 05-14-2007, 10:01 AM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM