Re: Multi-instructions macro and compiler warning
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Oct 2011 07:43:19 -0400
On 10/17/2011 12:58 PM, James Kuyper wrote:
On 10/17/2011 12:25 PM, christian.bau wrote:On Oct 17, 4:07 pm, ImpalerCore<jadil...@xxxxxxxxx> wrote:...On Oct 16, 10:40 am, "christian.bau"...all members to zero. Another is a warning for "if (x>= 0&& x<=
100)" if the type of x happens to be unsigned. )
If 'x' is unsigned, I would think that one could use 'if (/*x>= 0
&&*/ x<= 100)' to document the condition while removing the warning,
as the 'x>= 0' condition is implicit from the use of an unsigned
type.
The point is that the warning in this case is completely pointless.
No - it catches a very common mistake: forgetting that the expression
being tested is incapable, because of it's type, of failing the test.
This is very often a sign that the user wasn't thinking about the fact
that the expression was unsigned; and is often accompanied by code that
will not do what the author expected it to do, for that same reason.
That this was not true in this particular case doesn't invalidate
generation of the warning.
In a related case, a colleague once got a warning that
if (thing.field > 0) foo(); else bar();
.... was a vacuous test which would always result in calling bar(),
never foo(). Investigating, he found
struct { int field : 1; /*...*/ } thing;
The catch is that a plain `int' bit-field may be signed or unsigned
at the compiler's discretion. On the compiler where the code was first
developed such bit-fields were unsigned, so `field' could store either
zero or one and the `if' made sense. The compiler that issued the
warning treated bit-fields as signed, so `field' had one sign bit and
no value bits; its only possible (two's complement) values were zero
and minus one. Adding an `unsigned' fixed things.
Thus, the warning about restricted range led to the detection of
an actual portability bug that had been sitting in the code since
whoknowswhen. That's far from "completely pointless."
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Multi-instructions macro and compiler warning
- From: Keith Thompson
- Re: Multi-instructions macro and compiler warning
- References:
- Multi-instructions macro and compiler warning
- From: pozz
- Re: Multi-instructions macro and compiler warning
- From: christian.bau
- Re: Multi-instructions macro and compiler warning
- From: ImpalerCore
- Re: Multi-instructions macro and compiler warning
- From: christian.bau
- Re: Multi-instructions macro and compiler warning
- From: James Kuyper
- Multi-instructions macro and compiler warning
- Prev by Date: Re: Dennis Ritchie -- An Appreciation
- Next by Date: Re: Structure without a tag
- Previous by thread: Re: Multi-instructions macro and compiler warning
- Next by thread: Re: Multi-instructions macro and compiler warning
- Index(es):
Relevant Pages
|