Unusual warning

Discussion in 'C Programming' started by Arash Partow, Jun 1, 2007.

  1. Arash Partow

    Arash Partow Guest

    Hi all,

    I have the following bit of code:

    int main()
    {
    unsigned int a = 0xAAAAAAAA;
    unsigned int g = 0x55555555;
    a = (a <<= 1) ^ g;
    return 0;
    }

    GCC outputs the following warning:
    code.c: In function `main':
    code.c:5: warning: operation on `a' may be undefined


    I was wondering if anyone could explain why "a" may be undefined?
    Is this referenced somewhere in the cfaq?




    Arash Partow
    __________________________________________________
    Be one who knows what they don't know,
    Instead of being one who knows not what they don't know,
    Thinking they know everything about all things.
    http://www.partow.net
     
    Arash Partow, Jun 1, 2007
    #1
    1. Advertising

  2. Arash Partow said:

    > Hi all,
    >
    > I have the following bit of code:
    >
    > int main()
    > {
    > unsigned int a = 0xAAAAAAAA;
    > unsigned int g = 0x55555555;
    > a = (a <<= 1) ^ g;
    > return 0;
    > }
    >
    > GCC outputs the following warning:
    > code.c: In function `main':
    > code.c:5: warning: operation on `a' may be undefined
    >
    >
    > I was wondering if anyone could explain why "a" may be undefined?


    Because you're violating a "shall" that is not a constraint, to wit
    3.3(2):

    "Between the previous and next sequence point an object shall have
    its stored value modified at most once by the evaluation of an
    expression. Furthermore, the prior value shall be accessed only to
    determine the value to be stored."


    > Is this referenced somewhere in the cfaq?


    Not your exact expression, obviously, but the following URL:

    <http://c-faq.com/expr/seqpoints.html>

    covers the basic problem.

    I would guess that the following modification still expresses your
    intent and yet is well-defined:

    a = (a << 1) ^ g;

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 1, 2007
    #2
    1. Advertising

  3. Arash Partow

    Eric Sosman Guest

    Arash Partow wrote:
    > Hi all,
    >
    > I have the following bit of code:
    >
    > int main()
    > {
    > unsigned int a = 0xAAAAAAAA;
    > unsigned int g = 0x55555555;
    > a = (a <<= 1) ^ g;
    > return 0;
    > }
    >
    > GCC outputs the following warning:
    > code.c: In function `main':
    > code.c:5: warning: operation on `a' may be undefined
    >
    >
    > I was wondering if anyone could explain why "a" may be undefined?


    The variable `a' is well defined, but the operation
    on it is not. The fifth line tries to modify `a' twice
    without an intervening sequence point. That's undefined
    behavior. Perhaps you actually meant

    a = (a << 1) ^ g;

    .... which suffers from no objectionable udnfenideenss.

    > Is this referenced somewhere in the cfaq?


    Yes. Questions 3.1, 3.2, 3.3, and 3.3b all address
    this issue.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 1, 2007
    #3
    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. Pete Becker
    Replies:
    0
    Views:
    1,379
    Pete Becker
    Feb 10, 2005
  2. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,635
  3. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    319
  4. Derek Fountain

    Unusual warning - what's it trying tell me?

    Derek Fountain, Aug 22, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    86
    Derek Fountain
    Aug 22, 2005
  5. Julian Mehnle
    Replies:
    17
    Views:
    883
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page