Comparing signed and unsigned values

Discussion in 'C++' started by Christopher Benson-Manica, Nov 4, 2005.

  1. int main()
    {
    bool foo=false;
    for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
    bar=42;
    }
    return 0;
    }

    A certain C++ compiler I am using claims that there exists in the
    above code (on the line with the for loop) a comparison between a
    signed and unsigned value. g++ invoked with strict options remains
    silent. Is there any merit to the certain compiler's claim that
    signed and unsigned values are being compared?

    My guess is no, given that on

    int main( void )
    {
    for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
    bar=42;
    }
    return 0;
    }

    the same compiler is silent.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 4, 2005
    #1
    1. Advertising

  2. Christopher Benson-Manica

    REH Guest

    Christopher Benson-Manica wrote:
    > int main()
    > {
    > bool foo=false;
    > for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
    > bar=42;
    > }
    > return 0;
    > }
    >
    > A certain C++ compiler I am using claims that there exists in the
    > above code (on the line with the for loop) a comparison between a
    > signed and unsigned value. g++ invoked with strict options remains
    > silent. Is there any merit to the certain compiler's claim that
    > signed and unsigned values are being compared?
    >
    > My guess is no, given that on
    >
    > int main( void )
    > {
    > for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
    > bar=42;
    > }
    > return 0;
    > }
    >
    > the same compiler is silent.
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


    1 and 2 are signed constants. Try 1u and 2u.

    REH
     
    REH, Nov 4, 2005
    #2
    1. Advertising

  3. REH <> wrote:

    > 1 and 2 are signed constants. Try 1u and 2u.


    That did silence the warning, although I find it strange that it would
    warn on the code I posted and never on, say,

    for( unsigned int foo=0; foo < 5; foo++ );

    since foo < 5 should be the same signed/unsigned comparison that it
    complains about on the code I posted.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 4, 2005
    #3
  4. Christopher Benson-Manica wrote:

    > int main()
    > {
    > bool foo=false;
    > for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
    > bar=42;
    > }
    > return 0;
    > }
    >
    > A certain C++ compiler I am using claims that there exists in the
    > above code (on the line with the for loop) a comparison between a
    > signed and unsigned value.


    That's true. The result of the 'foo?1:2' expression is signed, while 'bar' is
    unsigned.

    > g++ invoked with strict options remains
    > silent.


    Language specification does not define situations when warnings should (or
    should not) be issued. Warnings are implementation-dependent. And they are
    largely a QoI issue.

    > Is there any merit to the certain compiler's claim that
    > signed and unsigned values are being compared?


    Yes. See above.

    > My guess is no, given that on
    >
    > int main( void )
    > {
    > for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
    > bar=42;
    > }
    > return 0;
    > }
    >
    > the same compiler is silent.


    In this case the compiler decided not to issue a waring (although technically it
    could), while in the previous case it decided to issue one. It might also depend
    on the compiler settings.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 4, 2005
    #4
  5. Christopher Benson-Manica wrote:
    > REH <> wrote:
    >
    >> 1 and 2 are signed constants. Try 1u and 2u.

    >
    > That did silence the warning, although I find it strange that it would
    > warn on the code I posted and never on, say,
    >
    > for( unsigned int foo=0; foo < 5; foo++ );
    >
    > since foo < 5 should be the same signed/unsigned comparison that it
    > complains about on the code I posted.
    >


    Apparently you just found the limit of that compiler's intelligence when it
    comes to analyzing situations like this. In this last example the signed value
    is an integral-constant expression and its value is known at compile time. The
    compiler can immediately see that the value is positive and that's it is safe to
    perform the comparison in unsigned context (as the language requires).

    In the original case the value of the expression depended on a non-constant
    boolean value, which means that the compiler could not immediately predict the
    actual signed value at compile time. Of course, if it could analyze it a bit
    deeper, it'd see either that 'foo' is always 'false' or that the two possible
    results '1' and '2' are both positive, which means that there was no problem in
    that case as well. But this compiler simply doesn't do this type of deeper
    analysis. It is, once again, a pure QoI issue.

    If it interests you, you might also want to try this

    const bool foo=false; // <- note the 'const'
    for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
    bar=42;

    and see what happens in this case.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 4, 2005
    #5
  6. Andrey Tarasevich <> wrote:

    > Language specification does not define situations when warnings should (or
    > should not) be issued. Warnings are implementation-dependent. And they are
    > largely a QoI issue.


    Yes, I'm aware of that; I have, however, learnt not to trust
    everything this particular C++ implementation tells me.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 4, 2005
    #6
  7. Andrey Tarasevich <> wrote:

    > If it interests you, you might also want to try this


    > const bool foo=false; // <- note the 'const'
    > for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
    > bar=42;


    > and see what happens in this case.


    It did interest me, and the compiler accepted it silently. Thanks for
    the insight.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 4, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zaki
    Replies:
    2
    Views:
    6,761
    Egbert Molenkamp
    Jun 30, 2004
  2. sridhar

    comparing unsigned long and unsigned int

    sridhar, Nov 1, 2004, in forum: C Programming
    Replies:
    6
    Views:
    471
    J. J. Farrell
    Nov 3, 2004
  3. Joe Van Dyk

    comparing signed and unsigned integers

    Joe Van Dyk, Jun 23, 2006, in forum: C Programming
    Replies:
    3
    Views:
    3,956
    Tom St Denis
    Jun 25, 2006
  4. Replies:
    3
    Views:
    458
    James Kanze
    Nov 19, 2008
  5. pozz
    Replies:
    12
    Views:
    791
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page