0

I am working on a project and I keep finding lines like this one:

if(pointer && pointer->whatever) {...}

Is it safe to assume that the if will carry on comparisons in the same order as described in the source code? Or may it happen that pointer->wathever is evaluated before (or concurrently) to the check of pointer against NULL?

user815129
  • 2,236
  • 2
  • 20
  • 40

4 Answers4

4

Yes this is a safe statement. Conditionals && and || are executed in short-circuit fashion in C.

If pointer is NULL then pointer->whatever will not be executed.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Can't say about `C` but for `C++` it holds only when `||` or `&&` used – qwm Jan 22 '14 at 12:08
  • @qwm I have changed it to those.But I actually can't think of other infix conditional operators in C... – Sergey L. Jan 22 '14 at 12:10
  • I've never heard the term "conditional operator", and "conditional" on its own usually refers to anything which is, well, dependent on a condition, without making statements about the shape of the condition. The more common term for what you mean are boolean operators. –  Jan 22 '14 at 12:13
  • @delnan `?:` is called a conditional operator. – Simple Jan 22 '14 at 12:35
  • @Simple Right. Though that's a different meaning than in this answer and as stupid as I think it is, the term "ternary operator" is more common. –  Jan 22 '14 at 12:48
2
if(pointer && pointer->whatever) 

Is like writing:

if(pointer) {
   if(pointer->whateve) {

   }
} 

So, yes. Read about Short-circuit evaluation.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I thought the compiler could potentially optimize the code in such a way that this would not hold true anymore – user815129 Jan 22 '14 at 12:07
  • @user815129 the compiler and even CPU may reorder the statements as such far as there are no side effects. So it might calculate already the memory offset of pointer->whatever as that in itself gives no issue. However it should not access that memory until the first statement is true. – KillianDS Jan 22 '14 at 12:08
  • @user815129 Then this answer would be wrong, since the usual interpretation of "code A is equivalent to code B" takes optimizations, undefined behavior etc. into account. Fortunately, it's not wrong. Which is why I'm surprised about the downvotes. –  Jan 22 '14 at 12:09
  • So this is wrong? I don't understand.... – Maroun Jan 22 '14 at 12:09
  • 1
    Why the downvotes? It's not exactly the same but that was also not stated. – KillianDS Jan 22 '14 at 12:10
  • 1
    The answer is wrong because if there were an else statement it would not get executed. – this Jan 22 '14 at 12:10
  • @user815129 This is *defined* in the C and C++ language definitions. All textbooks on these languages explain about this... – laune Jan 22 '14 at 12:11
  • @self. It'll be wrong as well if it was a potato. I answered the question as it is, no `else` there. – Maroun Jan 22 '14 at 12:11
2

The order of evaluation (for C and C++) of the && operator is left-to-right. Hence pointer will always be tested before any following statements. Any following statements will not be evaluated if pointer is evaluated false (short-circuiting).

suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Logical expressions are evaluated from Left-to-right and once the result became definitive it stops evaluating the other expressions. So if the expression pointer if false, pointer->whatever will not be evaluated. So it's abdolutly stafe to use it as you did.

rullof
  • 7,124
  • 6
  • 27
  • 36
  • 2
    You are mixing up associativity, order of evaluation and short circuiting. Being left associative only means that `f() && g() && h()` is interpreted as `(f() && g()) && h()`, with no guarantees about which function gets called first. Order of evaluation makes those guarantees about that. But left-to-right order of evaluation does not guarantee that the right operand is not evaluated if the left already determined the result. That's called short circuiting. –  Jan 22 '14 at 12:15