|
-
January 9th, 2011, 09:42 AM
#16
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.
-
January 9th, 2011, 10:13 AM
#17
Re: [c++]code executon
 Originally Posted by D_Drmmr
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.
-
January 9th, 2011, 10:51 AM
#18
Re: [c++]code executon
Thanks everybody for clearing this up.
-
January 9th, 2011, 07:47 PM
#19
Re: [c++]code executon
 Originally Posted by Chris_F
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.)
 Originally Posted by borko1980
...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.
-
January 9th, 2011, 11:04 PM
#20
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.
-
January 10th, 2011, 04:19 AM
#21
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|