473,506 Members | 16,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overflow

Hi all,

Suppose I have:

#include <climits>

const int val = INT_MAX;
val += 1;

Does the standard prescribe what happens with the above overflowing?
And if not, is there any *portable* way of discovering what happens?

Note: I've tested in my platform and the value val wrapps around.

TIA, with my best regards,
G. Rodrigues
Jul 23 '05 #1
4 1212
Gonçalo Rodrigues <op*****@mail.telepac.pt> writes:
Hi all, Suppose I have: #include <climits> const int val = INT_MAX;
val += 1; Does the standard prescribe what happens with the above overflowing?
And if not, is there any *portable* way of discovering what happens? Note: I've tested in my platform and the value val wrapps around.

Did you mean the const to be there? I get

Error 337: "foo.cc", line 5 # The left side of '+=' must be a modifiable lvalue.
val += 1;
^^^
Jul 23 '05 #2
Gonçalo Rodrigues schrieb:
Hi all,

Suppose I have:

#include <climits>

const int val = INT_MAX;
val += 1;

Does the standard prescribe what happens with the above overflowing?
And if not, is there any *portable* way of discovering what happens?

Note: I've tested in my platform and the value val wrapps around.

undefined behaviour
the standard even mentions systems which raise exceptions on
overflow(like x86 does on division by zero).
Jul 23 '05 #3
* Gonçalo Rodrigues:

Suppose I have:

#include <climits>

const int val = INT_MAX;
val += 1;

Does the standard prescribe what happens with the above overflowing?
No.

It does however specify overflow behavior for unsigned integer types.

Where the value is reduced modulo 2^n, n the number of value representation
bits.

And if not, is there any *portable* way of discovering what happens?
Partially. std::numeric_limits<int>::is_modulo tells you whether the type
has wrapping behavior or not. However, if it doesn't (on nearly all modern
C++ implementation it will be wrapping, but _if_ it doesn't wrap) then that
still leaves the question of what happens on overflow, open.

Note: I've tested in my platform and the value val wrapps around.


Not very surprising; google for "two's complement".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
On 9 Mar 2005 15:41:41 GMT, tp*@eng.cam.ac.uk (Tim Love) wrote:
Gonçalo Rodrigues <op*****@mail.telepac.pt> writes:
Hi all,

Suppose I have:

#include <climits>

const int val = INT_MAX;
val += 1;

Does the standard prescribe what happens with the above overflowing?
And if not, is there any *portable* way of discovering what happens?

Note: I've tested in my platform and the value val wrapps around.

Did you mean the const to be there? I get

Error 337: "foo.cc", line 5 # The left side of '+=' must be a modifiable lvalue.
val += 1;
^^^


Of course not, bad cut and paste.

Regards,
G. Rodrigues

Jul 23 '05 #5

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

Similar topics

15
4804
by: Andrew Fedoniouk | last post by:
I have a simple test document which produce the following in Mozilla and Opera: http://terrainformatica.com/w3/p2/problem1.png Internet Explorer behaves as per recommendation (I guess) Did I...
6
19380
by: Kai Grossjohann | last post by:
I have a frame which is supposed to contain a list of links. The clickable area of each link comprises a picture followed by some text. I want the text to be cut at the right hand side when the...
4
3543
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)) //...
19
3110
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
27
4502
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
7
2400
by: wij | last post by:
Hi: Is there better way of detecting multiplication overflow for type long than by using double precision of lldiv to verify the result? Thanks in advance. I.J.Wang
25
6217
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...
8
10647
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
2
2725
by: lexor | last post by:
Hi! I'm struggling with a strange IE behavior regarding tables. I have a table like the following one <table cellpadding="0" cellspacing="0" border="1" width="500"> <tr> <td><div...
42
6964
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
7103
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
7307
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,...
1
7021
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
7478
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
5614
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,...
1
5035
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...
0
4701
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
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 ...

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.