473,419 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Increment, decrement, overflow, and underflow

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
5 9181
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Oplec | last post by:
Hello, How can underflow and overflow of the basic arithmetic types be tested for using standard C++? For instance, if two long doubles are multiplied together, test whether or not the result...
9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
2
by: Paul Emmons | last post by:
Can anyone suggest example code, or how to proceed, in adding or subtracting two long long integers (I'm working in gcc with Linux, where a long long is 64 bits) and determining whether an overflow...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
4
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
11
by: pereges | last post by:
Hi, I have written a small function that can (I think) deal with overflow/underflow while adding integers and divide by zero problem. Can some one please tell me if I have done it correctly ? Would...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.