# If Statement Skipping

This topic is 4769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just a quick question... In C++, will the compiler completely skip over an if statment if there is nothing inside the if block? In other words, will the compiler not even evaluate an if statement such as this one:
if (x < 4){

}


because there is nothing inside the if block? I ask because I have an if statment in my program that whenever I put anything (doesn't matter what) inside of the if block, the program crashes, otherwise, everything is normal. The only explanation I can assume for this is that the compiler just doesn't care about the if statement anymore and isn't evaluating it. Anybody have confirmation on this? Thanks, Jeff EDIT: Fixed Tags

##### Share on other sites
If you have an if statement with nothing in it, and your compiler is optimising at all, that block will not show up in the compiled code, but will instead be optimized out.

##### Share on other sites
In your specific example, I doubt any compiler would fail to optimize away the comparison in a release build [read: with optimizations]. A quick check reveals that VS.NET2003, without optimizations set, evaluates the comparison.

CM

##### Share on other sites
Quote:
 Original post by GodlyGamrI ask because I have an if statment in my program that whenever I put anything (doesn't matter what) inside of the if block, the program crashes, otherwise, everything is normal.

Wow. You've got one serious problem if populating an conditional block causes a crash. About the only thing I can suggest is to do a make clean first, which would be like rebuild all in dev-c++.

##### Share on other sites
Quote:
 I ask because I have an if statment in my program that whenever I put anything (doesn't matter what) inside of the if block, the program crashes, otherwise, everything is normal.

Check for any memory access violations (indexing an out-of-bounds cell in an array, for instance) in your program. This is a possible cause.

To answer your original question, if the expression has no possible side-effects, then an optimizing compiler might not evaluate it at all. But remember that if your "if" statement is, for example
if (x++) {}

or
if (foo()) {}

it will always get evaluated.

##### Share on other sites
Quote:
 it will always get evaluated.

Sort of. The code will be executed, but the if itself will be ignored. if(foo()) {} should produce the exact same assembly as foo();

##### Share on other sites
Quote:
 Original post by eyalBut remember that if your "if" statement is, for exampleif (x++) {}orif (foo()) {}it will always get evaluated.

Not neccessarily. In particular, the if(x++) {} one can be optimized away entirely if you don't check the value of x again after that line. Its apparently called 'dead store removal.' I don't know all the rules, but I stumbled upon a discussion about that very issue a while back. This was part of it, but there were a few responses on another page I can't find anymore.

CM

##### Share on other sites
Thanks for the replies everybody. It must have been that my compiler was just ignoring it. I tried fumbling around with my code and found that it was most likely a NULL pointer exception in my conditional which was causing the crash.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
×