Thread: Strange side effects?

  1. #1
    Registered User
    Join Date
    Jun 2003

    Question Strange side effects?

    I'm using a compiler that is a lot more strict on the C language than normal (it's meant to help create less bugs in the code).

    One thing I'm confused at is that it complains about the following line of code:

    if ((timer == 0) && ((status & 0x100) == modval))


    Error: the right hand operand of an && or || operator should not contain side effects

    So, can anyone see the side effect it's complaining about? As I certainly can't. Also, why is it specifc about the right hand side?

    If I remove the "== modval" on the right hand side, I get the following additional error:

    Error: this bitwise operation is in a boolean context

    Which is understandable, as it doesn't evaluate to a proper boolean result, but rather assumes that true is anything apart from zero.

    Still doesn't explain why it thinks "status & 0x100" gives a side effect tho'

    EDIT: 'status' is a volatile. That explains it. Why only the right hand side though?
    Last edited by _Elixia_; 08-15-2005 at 02:58 AM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia
    In C, the right hand side of an && expression is guaranteed to be only evaluated if the left hand side is true. The left hand side will always be evaluated.

  3. #3
    Join Date
    May 2005
    What compiler are you using?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Error: the right hand operand of an && or || operator should not contain side effects
    As cwr states, the right hand side may NOT be evaluated, so any 'side' effects will not happen if the left hand side evaluates to false to start with.

    > EDIT: 'status' is a volatile.
    If this is mapped to some piece of hardware which detects whether it's been read or not (say), then knowing that it may not be read as often as your code suggests is probably something you need to know.

    You can alter the code like so
    temp = status; // force a read of the volatile
    if ((timer == 0) && ((temp & 0x100) == modval)) // evaluate using a non-volatile
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2003
    ^XOR: The compiler is IAR.

    Salem: Thanks, I did actually want the said behavior, but at least I know now /why/ the compiler was complaining. Since other people may want to edit the code once it's released, I think this is probably a good thing, as they may not be thinking about the code in the same way as me, and then create bugs with that misunderstanding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error int
    By faluiretoread in forum C++ Programming
    Replies: 10
    Last Post: 11-15-2008, 05:51 PM
  2. OpenGL example cube is black, white, grey only
    By edwardtisdale in forum Windows Programming
    Replies: 7
    Last Post: 09-22-2007, 02:37 PM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  5. Side effects
    By Eibro in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2003, 06:26 AM