Operation undefined

Discussion in 'C Programming' started by Christian Christmann, Jul 31, 2006.

  1. Hi,

    for this code

    int main( void )
    {
    unsigned short a= 10;
    a = (a <<= 1) ^ 4129; //warning concerns this line
    return 0;
    }

    gcc issues the warning:

    warning: operation on `a' may be undefined


    The problem is "(a <<=1 )" before the XOR operator, but
    why might this expression lead to undefined behavior?

    Regards,
    Chris
    Christian Christmann, Jul 31, 2006
    #1
    1. Advertising

  2. Christian Christmann wrote:
    > Hi,
    >
    > for this code
    >
    > int main( void )
    > {
    > unsigned short a= 10;
    > a = (a <<= 1) ^ 4129; //warning concerns this line
    > return 0;
    > }
    >
    > gcc issues the warning:
    >
    > warning: operation on `a' may be undefined
    >
    >
    > The problem is "(a <<=1 )" before the XOR operator, but
    > why might this expression lead to undefined behavior?


    The operation is undefined because a is modified twice without an
    intervening sequence point. Why not just use "a = (a << 1) ^ 4129" ?

    Robert Gamble
    Robert Gamble, Jul 31, 2006
    #2
    1. Advertising

  3. Christian Christmann wrote:
    > Hi,
    >
    > for this code
    >
    > int main( void )
    > {
    > unsigned short a= 10;
    > a = (a <<= 1) ^ 4129; //warning concerns this line
    > return 0;
    > }


    #include <stdio.h>

    int main(void)
    {
    unsigned short a = 10;
    #if 0
    /* the following is a variation on 'i = i++;' covered by the FAQ */
    a = (a <<= 1) ^ 4129;
    #endif
    /* but this should work */
    a = (a << 1) ^ 4129;
    printf("a was 10, and is now %u\n", a);
    /* and this should work */
    a = 10;
    a = (a << 1);
    a ^= 4129;
    printf("a was 10, and is now %u\n", a);
    return 0;
    }
    Martin Ambuhl, Jul 31, 2006
    #3
  4. On 2006-07-31, Christian Christmann <> wrote:
    > Hi,
    >
    > for this code
    >
    > int main( void )
    > {
    > unsigned short a= 10;
    > a = (a <<= 1) ^ 4129; //warning concerns this line
    > return 0;
    > }
    >
    > gcc issues the warning:
    >
    > warning: operation on `a' may be undefined
    >
    >
    > The problem is "(a <<=1 )" before the XOR operator, but
    > why might this expression lead to undefined behavior?
    >


    I'm pretty sure this is a (potential?) sequence-point error, and
    that is what gcc is warning you about. It appears to me that using
    (a <<= 1) is logically equivilant to (a << 1) in this context:

    int main (void)
    {
    unsigned short a = 10;
    a = (a << 1) ^ 4129;
    return 0;
    }

    --
    Andrew Poelstra <website down>
    To reach my email, use <email also down>
    New server ETA: 42
    Andrew Poelstra, Jul 31, 2006
    #4
    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. jimjim

    operation on `x' may be undefined?

    jimjim, Aug 10, 2005, in forum: C Programming
    Replies:
    30
    Views:
    1,529
    Robert Gamble
    Aug 11, 2005
  2. david ullua
    Replies:
    13
    Views:
    651
  3. raan
    Replies:
    2
    Views:
    440
  4. Buzz Lightyear
    Replies:
    10
    Views:
    1,107
    Alexander Bartolich
    Aug 12, 2009
  5. Prasoon Saurav
    Replies:
    1
    Views:
    500
Loading...

Share This Page