Short-circuit evaluation questions.
-
Take a look on the following code:
if ((A) && (B) && (C)) { ... }
If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant?
-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articles -
Take a look on the following code:
if ((A) && (B) && (C)) { ... }
If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant?
-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articlesI believe that the C language specifies that the short-circuit will always take place so that you can DEPEND upon this behavior to avoid problems. Consider this simple function (which is in our code now...)
BOOL IsEmpty(char* string) { if( string && string[0] ){ return FALSE; } return TRUE; }
I am relying upon the short circuit test of string as a pointer to be NON-NULL and the test failing, otherwise I might try to dereference memory I don't own! (Dereferencing a memory address at zero which will lead to the fatal 'access violation' exception on Windows) -
Take a look on the following code:
if ((A) && (B) && (C)) { ... }
If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant?
-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articlesrbid wrote: If A evaluates to FALSE Will B or C be evaluated? No. This is ALWAYS the case. rbid wrote: What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? Yes. rbid wrote: What about compilers that do optimizations, or their optimizations have been disabled? This is nothing to do with optimisations. It's in the standard. rbid wrote: What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? Yes, it's in the C standard.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Take a look on the following code:
if ((A) && (B) && (C)) { ... }
If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant?
-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articlesBe aware that if you define your own overloaded operators &&, || etc. that they will NOT short-circuit! Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"