This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Compiler warnings, overflow


On 07/31/2014 06:00 PM, Ricardo Telichevesky wrote:
> Hi, hope this is the right list.
> 
> Here is my code and output, at the bottom of the e-mail. y is
> "correct", w and z obviously have problems - multiplying two 32-bit
> integers "hoping" the result would be correct assigning to 64-bit -
> I guess it is the same problem as double oneThird= 1/3; the result
> being zero, and not 0.3333.
> 
> I was wondering if there is any strict warning that would flag the w
> and z assignments below, or the 1/3 above - the whole right hand
> side is evaluated as a 32-bit integer number, and assigned to a
> 64-bit integer or double. Not advocating this should be a default,
> but turning it on would help me detect some flaws in the code. Took
> me hours to catch a similar bug in my code, trying to solve a sparse
> system that has hundreds of millions of variables...

To quote the standard:

"A computation involving unsigned operands can never overflow, because
a result that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the largest
value that can be represented by the resulting type."

So, from C's point of view there is nothing to warn about.

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]