First of all, before I begin, I am using VC++ 2008 professional, running an Intel core2 on windows OS. I also know that this code will NEVER be executed on anything other than a core2/corei7 running Windows.
I have a while loop with 2 conditions that looks something like this: note: this is a much simplified version.
while((a != b) && (array[a] < c))
If the first condition (a != b)
generates a false, will the second condition even be evaluated? or will the loop just terminate right there?
I did a few tests and it seems that it is indeed true.
However, here is the catch. When and if first condition evaluates false, the second condition WILL generate an access violation if it is evaluated. However, from what i can see, once the first condition is evaluated as false, the program doesn't bother to evaluate the second condition and quits the loop, thus saving me.
The problem is that I can't quite get rid of the access violation problem without making my very nice and neat code suddenly blow up on me. However, due to the little "bug" (i know it's a compiler optimization, not a bug), I seem to be able to get away from it. I also know this is probably not good programming practice to do, but to be honest, in my situation, if it works, I'm already ahead of the game.
My question is, will this "bug" or programming malpractice come back and bite me some day? Even when considering that I tested the hell out of this section and WILL ONLY EVER USE IT THIS ONE TIME?
The second condition will not be evaluated unless the first one has been evaluated to true. You can count on this. Millions lines of code work because this is how C and C++ do short-curcuit logical expressions evaluation.
You can use it and count on it. If the first expression evaluates to false the second will not even start evaluating.
This is not a bug. C++ uses short-circuit evaluation, so the second condition will never be evaluated when the first condition is false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With