Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Increment, decrement, overflow, and underflow

Reply
Thread Tools

Increment, decrement, overflow, and underflow

 
 
Ian Pilcher
Guest
Posts: n/a
 
      01-20-2005
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-20-2005
Ian Pilcher <(E-Mail Removed)> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-20-2005
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

 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      01-20-2005
Ben Pfaff wrote:
> Ian Pilcher <(E-Mail Removed)> 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 (E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-21-2005
Ian Pilcher wrote:
> Ben Pfaff wrote:
> > Ian Pilcher <(E-Mail Removed)> 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-21-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
use of control87 and floating point underflow jobs239@hotmail.com C Programming 2 08-23-2006 09:14 PM
Underflow and floating-point math tings C Programming 13 06-07-2005 02:58 PM
high precision Math with 'double' (underflow problem) Wolfgang Java 4 02-24-2004 03:41 AM
The difference of uflow and underflow functions ? C++ 2 11-19-2003 01:15 PM
streambuf::underflow() infinite loop Davis King C++ 0 07-23-2003 02:37 AM



Advertisments