473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4516
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 "mathematic al"
value is outside the type's range. "Discontinu ity" 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
4342
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; there is overflow and the result is not what I want. If such sum calculation scatters around my code, I wonder what's the best way to catch it?
44
8573
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 implementations. But then you have signed integers. Let's say a signed integer is set to its maximum positive value. If you add 1 to it, what happens?: A) It's implementation defined what value it will
4
3554
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)) // overflow else // not overflow
34
16689
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 - 3;
25
2721
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 as a double, even that has its limits. Is there a method of preventing this overflow or some method of recovering from it. Any help in this regard would be greatly appreciated. Thanking you.
25
6261
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 something;
3
4586
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 addition(unsigned int i1, unsigned int i2) { if ( i1 + i2 < i1 || i1 + i2 < i2 ) { ...throw some expception... } return i1+i2; }
4
9951
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 (augend + addend); if (sum < augend)
17
7561
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
7028
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 type 'long' is used. That way native C speed can be reached. Now I want to experiment with raising a Seed7 exception (which is emulated with setjmp(), longjmp() in C) for integer
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.