This is the mail archive of the
mailing list for the GCC project.
Re: Warning flags for unsigned operations (unsafe)
- From: "Luca Masini" <luca dot masini at t-online dot de>
- To: Dave Korn <dk at artimi dot com>, 'Mathieu Malaterre' <mmalater at nycap dot rr dot com>, gcc at gcc dot gnu dot org
- Date: Wed, 22 Sep 2004 22:35:25 +0200
- Subject: Re: Warning flags for unsigned operations (unsafe)
- References: <NUTMEGQp8PUG30GwOFO0000000b@NUTMEG.CAM.ARTIMI.COM>
On Wed, 22 Sep 2004 14:36:36 +0100, Dave Korn <email@example.com> wrote:
I have been googling around and I couldn't find out if
gcc had a warning flag for unsigned operation. For example, even the
interpolation on [a,b] can be tricky to code:
1. c = a + t * (b - a); //unsafe
2. c = (1.0 - t) * a + t * b; //safe
Number 1 will fail when both a and b are unsigned and let say
b - a = -1
(math speaking). Is there something in gcc that could warn me
for this kind of operation ?
Your code has a design flaw and is not valid. If you want to do maths
that involves negative quantities, you HAVE to use a signed variable,
not an unsigned one. If you want to do subtraction with unsigned
have it work, you have to ensure (by a test) to always subtract the
smaller from the larger.
I'm curious too, and interested on the original question.
GCC has the warning for that or not ?