We know that logical-AND operator (&&
) guarantees left-to-right evaluation.
But I am wondering if the compiler optimizer can ever reorder the memory access instructions for *a
and b->foo
in the following code, i.e. the optimizer writes instructions that try to access *b
before accessing *a
.
(Consider both a
and b
to be pointers to memory regions in the heap.)
if (*a && b->foo) {
/* do something */
}
One might think that &&
causes a sequence point, so the compiler must emit instructions to access *a
before accessing *b
but after reading the accepted answer at https://stackoverflow.com/a/14983432/1175080, I am not so sure. If you look at this answer, there are semi-colons between statements and they also establish sequence points and therefore they should also prevent reordering, but the answer there seems to indicate that they need compiler level memory barrier despite the presence of semicolons.
I mean if you claim that &&
establishes a sequence point, then that is true for semicolons in the code at https://stackoverflow.com/a/14983432/1175080. Then why is a compiler-level memory barrier required in that code?