473,785 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer overflow

I believe there can be an integer overflow, without a silent
wrap-around, in the following example:

int a = INT_MAX;
a++;

Am I right? Could this lead to an abnormal program termination in some
implementations ?

If so, could this happen without an arithmetical operation, i.e. because
of an assignment between different-ranged integers? I mean:

int a = 12345;
char b = a;

Or is it just an arithmetical operation issue?

Nov 13 '05 #1
9 5372
Mike Wahler wrote:
Enrico 'Trippo' Porreca <tr****@lombard iacom.it> wrote in message
news:3F******** ****@lombardiac om.it...
I believe there can be an integer overflow,


Yes, there can. When it happens, the language deems
it 'undefined behavior', which means anything is
allowed to happen.


Perfect.
If so, could this happen without an arithmetical operation, i.e. because
of an assignment between different-ranged integers? I mean:

int a = 12345;
char b = a;


If 12345 is outside the range of type 'char', then
data will be lost.


Suppose 12345 is indeed outside the range of 'char' on my platform. Do I
get undefined behaviour then? I guess b can contain almost anything
(from the point of view of the C standard).

Nov 13 '05 #2
Enrico 'Trippo' Porreca wrote:

Mike Wahler wrote:
Enrico 'Trippo' Porreca <tr****@lombard iacom.it> wrote in message
news:3F******** ****@lombardiac om.it...
I believe there can be an integer overflow,


Yes, there can. When it happens, the language deems
it 'undefined behavior', which means anything is
allowed to happen.


Perfect.
If so, could this happen without an arithmetical operation, i.e. because
of an assignment between different-ranged integers? I mean:

int a = 12345;
char b = a;


If 12345 is outside the range of type 'char', then
data will be lost.


Suppose 12345 is indeed outside the range of 'char' on my platform. Do I
get undefined behaviour then? I guess b can contain almost anything
(from the point of view of the C standard).


From a "legalistic " standpoint, the Standard permits one
of two outcomes: Either `b' receives an implementation-defined
value, or an implementation-defined signal is raised.
From
a practical standpoint, nearly every implementation takes the
former course, silently delivering some kind of value to `b'.
(It's been thirty years since I last used a machine where the
option of raising a signal might have made sense -- and that
machine didn't have a C implementation. )

Note that the situation is a bit different for unsigned
integral types. `unsigned char c = a;' would not raise any
signal, and would always deliver a value to `c'. The exact
value would be implementation-defined because different
implementations can have differing numbers of bits in their
`char' objects, but once the implementation-specific value
of `UCHAR_MAX' is known, the value stored in `c' can be
predicted with certainty.

--
Er*********@sun .com
Nov 13 '05 #3
Eric Sosman wrote:
From a "legalistic " standpoint, the Standard permits one
of two outcomes: Either `b' receives an implementation-defined
value, or an implementation-defined signal is raised.
From
a practical standpoint, nearly every implementation takes the
former course, silently delivering some kind of value to `b'.
(It's been thirty years since I last used a machine where the
option of raising a signal might have made sense -- and that
machine didn't have a C implementation. )


Now it's perfectly clear. Thank you very much.

Nov 13 '05 #4
Enrico 'Trippo' Porreca wrote:

I believe there can be an integer overflow, without a silent
wrap-around, in the following example:

int a = INT_MAX;
a++;

Am I right? Could this lead to an abnormal program termination
in some implementations ?
Yes.

If so, could this happen without an arithmetical operation,
i.e. because of an assignment between different-ranged
integers? I mean:

int a = 12345;
char b = a;


Again, yes. While many implementations silently convert such
overflows into nonsensical values, you cannot assume such. The
actual behaviour is undefined.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #5
CBFalconer <cb********@yah oo.com> wrote in message news:<3F******* ********@yahoo. com>...
Enrico 'Trippo' Porreca wrote:

....
int a = 12345;
char b = a;


Again, yes. While many implementations silently convert such
overflows into nonsensical values, you cannot assume such. The
actual behaviour is undefined.


No. If char is unsigned, the behaviour is perfectly well defined.

If char is signed then the behaviour is implementation defined on C90
implementations .

--
Peter
Nov 13 '05 #6
Peter Nilsson wrote:
CBFalconer <cb********@yah oo.com> wrote:
Enrico 'Trippo' Porreca wrote:

...
int a = 12345;
char b = a;


Again, yes. While many implementations silently convert such
overflows into nonsensical values, you cannot assume such. The
actual behaviour is undefined.


No. If char is unsigned, the behaviour is perfectly well defined.

If char is signed then the behaviour is implementation defined on
C90 implementations .


Touche. chars are different.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #7
CBFalconer <cb********@yah oo.com> wrote:
Peter Nilsson wrote:
CBFalconer <cb********@yah oo.com> wrote:
> Enrico 'Trippo' Porreca wrote:

...
> > int a = 12345;
> > char b = a;
>
> Again, yes. While many implementations silently convert such
> overflows into nonsensical values, you cannot assume such. The
> actual behaviour is undefined.


No. If char is unsigned, the behaviour is perfectly well defined.

If char is signed then the behaviour is implementation defined on
C90 implementations .


Touche. chars are different.


It's nothing to do with chars. Integer overflow causes undefined
behaviour, but a conversion of a value that can't fit into the target
type is implementation-defined behaviour in C90. This has been gone
over on this newsgroup many times.

- Kevin.

Nov 13 '05 #8
Kevin Easton wrote:

CBFalconer <cb********@yah oo.com> wrote:
Peter Nilsson wrote:
CBFalconer <cb********@yah oo.com> wrote:
> Enrico 'Trippo' Porreca wrote:
...
> > int a = 12345;
> > char b = a;
>
> Again, yes. While many implementations silently convert such
> overflows into nonsensical values, you cannot assume such. The
> actual behaviour is undefined.

No. If char is unsigned, the behaviour is perfectly well defined.

If char is signed then the behaviour is implementation defined on
C90 implementations .


Touche. chars are different.


It's nothing to do with chars. Integer overflow causes undefined
behaviour, but a conversion of a value that can't fit into the target
type is implementation-defined behaviour in C90. This has been gone
over on this newsgroup many times.


Did I say anything else? I simply accepted the correction that an
overflow into a char, be it plain, signed, unsigned, is different
from an integer overflow.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #9
pete wrote:

CBFalconer wrote:

Kevin Easton wrote:

CBFalconer <cb********@yah oo.com> wrote:
> Peter Nilsson wrote:
>> CBFalconer <cb********@yah oo.com> wrote:
>> > Enrico 'Trippo' Porreca wrote:
>> ...
>> > > int a = 12345;
>> > > char b = a;
You used the word "overflow", when the code in question
merely has the assignement of an out of range value
(assuming that 12345 is out of range for char in this case).
The assignement of an out of range value,
is different from overflow and
the behavior is implementation defined, not undefined.


.... and also assuming that char is signed.
If unsigned, then the behavior is defined.

--
pete
Nov 13 '05 #10

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

Similar topics

1
4344
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?
2
648
by: REH | last post by:
If the is_modulo field of the numeric_limits class is true for signed integer types, can I assume that overflow for such types is defined behavior? If so, is the behavior the same regardless of implementation? Also, if the range of an integer type is not symmetrical around zero (i.e., 2's comp.), is it safe to assume that the extra value(s) is one the negative side? Thanks,
25
2726
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
6276
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;
9
8636
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is FFFFFFDB, which should be -37. Thanks.
40
2815
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at: http://www.cert.org/secure-coding/ The purpose of this library is to provide a collection of utility functions that can assist software developers in writing C programs that are free from common integer problems such as integer overflow, integer...
13
3227
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
42
7036
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
6
5000
by: Chris Becke | last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in calculations. When multiplying two numbers, the CPU automatically returns a double width result. c & c++ give programmers these bloody ridiculous integer types of undetermined size and no really optimal way to even build a variable width number library...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
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
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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
6740
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.