473,396 Members | 1,858 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,396 software developers and data experts.

overflow of unsigned int

Suppose

unsigned int size = UINT_MAX;

Now consider

++size;

After increment operator, size value becomes zero in

1) VC++ 2005 Express Edition on Intel Pentium D and
2) Redhat Enterprise Linux gcc on Intel Pentium D

Will it be possible to tell whether this behaviour(ie becoming 0) be
the same on all machines and all standard C compliant compilers. Is
this a question about overflow ? I am reading K & R second edition.
Are there any internet links that I should read material to know
more ?

Thanks

Mar 9 '07 #1
3 4489
su**************@yahoo.com, India said:
Suppose

unsigned int size = UINT_MAX;

Now consider

++size;

After increment operator, size value becomes zero in
....all conforming implementations.
>
1) VC++ 2005 Express Edition on Intel Pentium D and
2) Redhat Enterprise Linux gcc on Intel Pentium D

Will it be possible to tell whether this behaviour(ie becoming 0) be
the same on all machines and all standard C compliant compilers.
Yes.
Is this a question about overflow ?
No. Unsigned integer types cannot overflow. All unsigned integer
arithmetic is performed modulo one greater than the largest value that
the type can store.
I am reading K & R second edition.
Good.
Are there any internet links that I should read material to know
more ?
<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdfis a draft
of the latest C Standard plus revisions. Also, you may like to consider
checking out some other relevant links about learning C, as listed at
<http://www.cpax.org.uk/prg/portable/c/resources.php>.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #2
su**************@yahoo.com, India wrote:
Suppose

unsigned int size = UINT_MAX;

Now consider

++size;

After increment operator, size value becomes zero in

1) VC++ 2005 Express Edition on Intel Pentium D and
2) Redhat Enterprise Linux gcc on Intel Pentium D

Will it be possible to tell whether this behaviour(ie becoming 0) be
the same on all machines and all standard C compliant compilers.
Yes, it is consistent across all conformant C implementations.
Is this a question about overflow ?
No, the C standard ascribes modulo 2^n arithmetic for unsigned types,
which by definition, has no overflow. Rather it produces a ring with
wrap-around of the value. Overflow is defined only for signed integral
and floating point types.

Mar 9 '07 #3
Richard Heathfield wrote On 03/09/07 07:37,:
[...]
Unsigned integer types cannot overflow. [...]
This is literally true, but it seems to be a point of
confusion: the matter arises in c.l.c. again and again and
again. Perhaps we need a term to describe the non-overflow-
but-possibly-surprising thing that happens when unsigned
arithmetic operates on an expression whose "mathematical"
value is outside the type's range. "Discontinuity" doesn't
seem appropriate, but "wraparound" (in various forms) has
been used before and seems to convey the right meaning.
Richard's oft-repeated statement might be a little more
informative to the befuddled if it read

Unsigned integer types cannot overflow;
they wrap around.

My own preference would be to focus on the arithmetic
rather than on the type, and say

Unsigned arithmetic cannot overflow; it
wraps around.

.... but that's a minor stylistic point about which reasonable
people can disagree in amity.

(In the post I quoted, Richard goes on to describe
"wrap around" precisely, in terms of modular arithmetic.
That's still helpful, but perhaps "wrap around" would give
the confused reader an up-front handle for understanding.)

Thoughts?

--
Er*********@sun.com
Mar 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Black | last post by:
Hi, If I have many integer calculation in my code, what's the best way to detect integer overflow? unsigned int i1 = 0xFFFFFF00, i2 = 0xFFFF; then in statement unsigned int i3 = i1 + i2;...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
4
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) //...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
25
by: Ashutosh Iddya | last post by:
Hi , I am performing an integer count of a particular operation in my program. After a sufficiently large value an overflow occurs. At the moment I have gone around the problem by declaring it...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
3
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int...
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...
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.