Overflow errors?

A

amit.codename13

I am not sure which one of the following errors are examples of
integer overflow...
by integer overflow i mean the one that the C99 standard states would
invoke undefined behavior...

1> int i=INT_MAX+1;

2> int i=INT_MOREMAX;

where INT_MOREMAX is a macro defined to have a value greater than
INT_MAX;

if i am not wrong, integer overflow can occur in the creation of the
value that would be assigned to i...

and where does the standard mention (if it is true) that INT_MAX is
the limit crossing which can cause a integer overflow... why not
LONG_MAX ?
 
B

Ben Bacarisse

I am not sure which one of the following errors are examples of
integer overflow...
by integer overflow i mean the one that the C99 standard states would
invoke undefined behavior...

1> int i=INT_MAX+1;
Yes.

2> int i=INT_MOREMAX;

where INT_MOREMAX is a macro defined to have a value greater than
INT_MAX;

Maybe not. Without seeing INT_MOREMAX this is a guess. If it is
defined as INT_MAX+1 then it is the same as the first, of course. If,
however, it is LONG_MAX and LONG_MAX > INT_MAX, then you don't get
overflow.
if i am not wrong, integer overflow can occur in the creation of the
value that would be assigned to i...

The second is probably a conversion, the first is arithmetic. The
effect of converting a value that can't be represented as an int is
"implementation-defined" or that "an implementation-defined signal is
raised" (6.3.1.3 p3). The effect is that neither line is portable or
"safe" but there is a technical difference.
and where does the standard mention (if it is true) that INT_MAX is
the limit crossing which can cause a integer overflow... why not
LONG_MAX?

It is INT_MAX because neither INT_MAX nor 1 cause a promotion to
anything but int (most of section 6.3.1). Of course LONG_MAX may
equal INT_MAX but I know what you mean.
 
J

jameskuyper

I am not sure which one of the following errors are examples of
integer overflow...
by integer overflow i mean the one that the C99 standard states would
invoke undefined behavior...

1> int i=INT_MAX+1;

This is an expression whose type is int, and whose mathematical value
lies outside the range of possible values for an int. This is covered
by 6.5p5: "If an exceptional condition occurs during the evaluation of
an expression (that is, if the result is not mathematically defined or
not in the range of representable values for its type), the behavior
is undefined." Note: "exceptional condition" is in italic in the
original text, which is the standard's way of indicating that this
particular clause is the one that defines what "exceptional condition"
means.
2> int i=INT_MOREMAX;

where INT_MOREMAX is a macro defined to have a value greater than
INT_MAX;

Inherently, INT_MOREMAX must have a type for which it's value is in
range; that type is therefore not 'int'. Therefore, what's involved
here is a conversion from one type to another of a value that is
outside the range of the destination type. This is covered by 6.3.1.3:

"1 ... if the value can be represented by the new type, ...
2 ... if the new type is unsigned, ...
3 Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined or an
implementation-defined signal is raised."

Since INT_MOREMAX cannot be represented as an int, and because 'int'
is a signed type, paragraph 3 applies.

For practical purposes, producing an implementation-defined result
renders the calculation pointless, and in code intended to be
portable, raising an implementation-defined signal is pretty much
indistinguishable from having undefined behavior.

....
and where does the standard mention (if it is true) that INT_MAX is
the limit crossing which can cause a integer overflow... why not
LONG_MAX ?

5.2.4.2.1, where it specifies that INT_MAX is the "maximum value for
an object of type int". If INT_MAX were defined with a value such that
the mathematical value of INT_MAX+1 could be stored in an int, then
INT_MAX would not be the largest value that can be stored in an int.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top