Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short circuit evaluation and side effects [duplicate]

Tags:

c

OK, I'm a little embarassed to ask this question, but I just want to be sure...

It is known that C uses short circuit evaluation in boolean expressions:

int c = 0;
if (c && func(c)) { /* whatever... */ }

In that example func(c) is not called because c evaluates to 0. But how about more sophisticated example where side effects of comparison would change the variable being compared next? Like this:

int c; /* this is not even initialized... */
if (canInitWithSomeValue(&c) && c == SOMETHING) { /*...*/ }

Function canInitWithSomeValue returns true and changes value at given pointer in case of success. Is it guaranteed that subsequent comparisons (c == SOMETHING in this example) uses value set by canInitWithSomeValue(&c)?

No matter how heavy optimizations the compiler uses?

like image 626
Łukasz Sromek Avatar asked Sep 03 '10 12:09

Łukasz Sromek


People also ask

What is a side effect and how does it relate to short-circuit evaluation?

Short-circuit evaluation is not safe when the skipped does more than that. When a method makes permanent changes to data, it must be called regardless of the true/false value of some other method. When a method makes a permanent change to data the method is said to have a side effect.

What are the potential problems about short-circuit evaluation?

Disadvantages Of Short-Circuit Evaluation:Code execution becomes less efficient with short-circuited execution paths because in some compilers the new checks for short-circuits are extra execution cycles in themselves.

What is the purpose of short-circuit evaluation?

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first ...

What is short-circuit evaluation for AND and OR operators?

Short-circuit evaluation The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false , the AND operator stops and returns the original value of that falsy operand; it does not evaluate any of the remaining operands.


1 Answers

Is it guaranteed that subsequent comparisons (c == SOMETHING in this example) uses value set by canInitWithSomeValue(&c)?

Yes. Because there is a sequence point

Between evaluation of the left and right operands of the && (logical AND), || (logical OR), and comma operators. For example, in the expression *p++ != 0 && *q++ != 0, all side effects of the sub-expression *p++ != 0 are completed before any attempt to access q.


A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

like image 149
Amarghosh Avatar answered Sep 21 '22 04:09

Amarghosh