CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: [c++]code executon

    There wouldn't be much point to an if statement if things inside its block got executed whether it was true or not. This isn't a case of "probably not" or it depends on optimizations, it definitely doesn't. It would probably easier to verify what your code is doing with the debugger than to try to figure out the assembly.

  2. #17
    Join Date
    May 2009
    Posts
    2,413

    Re: [c++]code executon

    Quote Originally Posted by D_Drmmr View Post
    If statement1 is false, statement2 will not be executed. Similarly for || (logical OR).
    Just to be clear.

    In the || case, statement2 will not be executed if statement1 is true.

    The short-cut rule states that the right-hand side will not be evaluated if the evaluation of the left-hand side is sufficient to determine the outcome of the whole expression.
    Last edited by nuzzle; January 9th, 2011 at 10:16 AM.

  3. #18
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    Thanks everybody for clearing this up.

  4. #19
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: [c++]code executon

    Quote Originally Posted by Chris_F View Post
    It could be.

    e.g.

    Code:
    if(var1 == 42)
    {
        if(var2 == 42)
        {
            do_something()
        }
    }
    could be better assembled as:

    Code:
    //pseudo 
    mov reg, var1
    and reg, var2
    cmp reg, 42
    je _do_something
    Definitely not. The two are not even equivalent. Your assembly snippet would take the "true" route if all bits set in both variables give the value of 42 (== 0x2A). That would be equivalent to the following C++ snippet:

    Code:
    if ((var1 & var2) == 42) {
      // ...
    }
    It also would violate the short circuit evaluation rule as pointed out by D_Drmmr which could be fatal if var2 was replaced by something that has side effects. (But maybe you meant that as an optimization that would not be done it var2 had side effects, so my objection would be less relevant.)

    Quote Originally Posted by borko1980 View Post
    ...look at the compiled code.
    In case of my compiler, where?
    FYI, VC++ has an option to generate an assembly listing of the compiled code for you. You'll find it in the project properties under C/C++ -> Output Files. IIRC the generated listing file will have the file name extension .cod.
    Last edited by Eri523; January 9th, 2011 at 07:50 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #20
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    But maybe you meant that as an optimization that would not be done it var2 had side effects, so my objection would be less relevant.
    That's more or less what I meant. I realize it's a bad example. It's more likely that the if statements and variables would simply be removed all together through optimization anyway.

  6. #21
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: [c++]code executon

    Not sure how other compilers deal with that issue, but MS compilers handle the individual expressions in an if clause as sequence points if they´re combined by && or ||. This guarantees that the expressions are evaluated from left to right, making sure that code like

    Code:
    if( ptr && ptr->func() )
    {
       ...
    }
    does not produce UB when ptr is 0.
    - Guido

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured