Undefined operation on a pointer variable
Problem: I received the following warning: operation on ‘vq’ may be undefined [-Wsequence-point]
Solution: The GNU Compiler Collection (GCC) 4.6.3 issues a warning when the sequence order of operations is ambiguous.
In the following example, the vq pointer variable
is both set and compared on the same statement. It might not be immediately
clear whether the vq pointer variable is updated
first and then used in the comparison, or used in the comparison and
then updated. Because of the precedence and associativity of operators, == has a higher order of precedence than =, so the compiler does the comparison first
and then updates the vq pointer variable. This
might or might not be what was intended. The undefined operation [-Wsequence-point] warning is enabled by specifying the -Wall compiler option for C/C++ language.
if ((vq = *vp->v_hashchain) == vq->v_specnext)
{
…
}
Rewriting the code eliminates the compiler warning. In the following
example, rewriting the code also has the advantage of removing the
ambiguity.
vq = *vp->v_hashchain;
if (vq == vq->v_specnext)
{
…
}
Note: The -Wsequence-point warning can be valuable;
therefore, do not suppress the warning by using the -Wno-sequence-point compiler option unless you are sure that your code is correct.