By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,967 Members | 2,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,967 IT Pros & Developers. It's quick & easy.

Increment, decrement, overflow, and underflow

P: n/a
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 i.*******@comcast.net
================================================== ======================
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ian Pilcher <i.*******@comcast.net> 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;}
Nov 14 '05 #2

P: n/a
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

Nov 14 '05 #3

P: n/a
Ben Pfaff wrote:
Ian Pilcher <i.*******@comcast.net> 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 i.*******@comcast.net
================================================== ======================
Nov 14 '05 #4

P: n/a
Ian Pilcher wrote:
Ben Pfaff wrote:
Ian Pilcher <i.*******@comcast.net> 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

Nov 14 '05 #5

P: n/a
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

Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.