Increment, decrement, overflow, and underflow

Discussion in 'C Programming' started by Ian Pilcher, Jan 20, 2005.

  1. Ian Pilcher

    Ian Pilcher Guest

    I'm trying to figure out if an increment to a variable of an integer
    type, followed by a decrement, (or vice versa) is guaranteed to restore
    the variable to its initial value, even if the first operation causes
    the variable to overflow/underflow.

    In other words, if foo_t is an integer type, are the following two
    functions guaranteed to *always* return a non-zero value?

    int check_overflow(foo_t f)
    {
    foo_t g = f;
    g++;
    g--;
    return (f == g);
    }

    int check_underflow(foo_t f)
    {
    foo_t g = f;
    g--;
    g++;
    return (f == g);
    }

    Thanks!

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Jan 20, 2005
    #1
    1. Advertising

  2. Ian Pilcher

    Ben Pfaff Guest

    Ian Pilcher <> writes:

    > I'm trying to figure out if an increment to a variable of an integer
    > type, followed by a decrement, (or vice versa) is guaranteed to restore
    > the variable to its initial value, even if the first operation causes
    > the variable to overflow/underflow.


    This is only true for unsigned integer types, which are
    guaranteed to "wrap around". Signed integer types might signal
    an error on overflow or underflow.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Jan 20, 2005
    #2
    1. Advertising

  3. Ian Pilcher wrote:
    > I'm trying to figure out if an increment to a variable of an
    > integer type, followed by a decrement, (or vice versa) is
    > guaranteed to restore the variable to its initial value, even
    > if the first operation causes the variable to overflow/underflow.


    For signed integers, no. You can't undo the effects of undefined
    behaviour once it's happened. You have to pre-emptively detect
    that an overflow will occur and deal with it before the
    operation in question.

    > In other words, if foo_t is an integer type, are the following two
    > functions guaranteed to *always* return a non-zero value?
    >
    > int check_overflow(foo_t f)
    > {
    > foo_t g = f;
    > g++;
    > g--;
    > return (f == g);
    > }


    If f < FOO_MAX, or if foo_t is unsigned and of rank equal or
    higher than int, or if foo_t promotes to unsigned int, or if
    foo_t promotes to int and FOO_MAX < INT_MAX, then yes.
    Otherwise, no.

    > <snip similar function>


    --
    Peter
    Peter Nilsson, Jan 20, 2005
    #3
  4. Ian Pilcher

    Ian Pilcher Guest

    Ben Pfaff wrote:
    > Ian Pilcher <> writes:
    >
    >
    >>I'm trying to figure out if an increment to a variable of an integer
    >>type, followed by a decrement, (or vice versa) is guaranteed to restore
    >>the variable to its initial value, even if the first operation causes
    >>the variable to overflow/underflow.

    >
    >
    > This is only true for unsigned integer types, which are
    > guaranteed to "wrap around". Signed integer types might signal
    > an error on overflow or underflow.



    Thanks for the response. I momentarily forgot about trap
    representations.

    Fortunately, I'm currently worried about decrementing a size_t which may
    be 0, so I guess I'm OK.

    Thanks!

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Jan 20, 2005
    #4
  5. Ian Pilcher wrote:
    > Ben Pfaff wrote:
    > > Ian Pilcher <> writes:
    > >
    > > > I'm trying to figure out if an increment to a variable of
    > > > an integer type, followed by a decrement, (or vice versa)
    > > > is guaranteed to restore the variable to its initial value,
    > > > even if the first operation causes the variable to
    > > > overflow/underflow.

    > >
    > > This is only true for unsigned integer types, which are
    > > guaranteed to "wrap around". Signed integer types might signal
    > > an error on overflow or underflow.

    >
    > Thanks for the response. I momentarily forgot about trap
    > representations.


    Trap representations are irrelevant for arithmetic operations.

    > Fortunately, I'm currently worried about decrementing a size_t
    > which may be 0, so I guess I'm OK.


    Yes. If it is 0, then a decrement will produce SIZE_MAX.

    [Note however that incrementing a size_t object with value
    SIZE_MAX is _theoretically_ problematic since size_t may
    have rank lower than int, and SIZE_MAX == INT_MAX is a
    possibility. This is highly unlikely in practical
    implementations though. I.e. it's one those "well worth
    ignoring" elements of standard C.]

    --
    Peter
    Peter Nilsson, Jan 21, 2005
    #5
  6. Ian Pilcher

    CBFalconer Guest

    Ian Pilcher wrote:
    >
    > I'm trying to figure out if an increment to a variable of an integer
    > type, followed by a decrement, (or vice versa) is guaranteed to restore
    > the variable to its initial value, even if the first operation causes
    > the variable to overflow/underflow.


    No, not for signed types. For unsigned, yes. The result of signed
    overflow is undefined behavior, which means anything at all may
    occur, including what you want.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jan 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrix
    Replies:
    1
    Views:
    1,665
    Ivan Vecerina
    Jun 29, 2003
  2. Mark Turney
    Replies:
    11
    Views:
    4,263
    dibeas
    Nov 13, 2006
  3. John
    Replies:
    4
    Views:
    1,129
    Vladimir Marko
    May 17, 2005
  4. lovecreatesbeauty
    Replies:
    8
    Views:
    1,629
    Old Wolf
    Sep 12, 2005
  5. Angel Tsankov
    Replies:
    8
    Views:
    459
    Ian Collins
    Feb 27, 2006
Loading...

Share This Page