conditional expression evaluation question

George M. Sipe george at rebel.UUCP
Tue Jan 13 07:40:40 AEST 1987


References:


I need to check a string, composed of byte triples, for a null area no
less than MINSKIP triples in length.  A pointer, cp, is initialized to
a triplet boundary.  After the test, it must remain on a triplet
boundary.  Initially, I wrote the following:

	while (cp < end && triples < MINSKIP)
		if ((*cp++ | *cp++ | *cp++) == 0) ++triples;
		else triples = 0;

After looking at it, I wasn't absolutely sure that it would perform as
expected.  My question is "Does C guarantee execution of portions of a
conditional expression, even when the result is known after partial
evaluation?".  In my example, if the first byte is non-null, then the
result is known to be false and there is no need to evaluate the
remaining subexpressions.  However, I am counting on the whole
expression being evaluated.  Assuming that C does guarantee full
execution of the conditional test, would I be taking a significant
chance that an optimizing compiler might be too tricky and
(incorrectly) fail to do the full evaluation?

For now, I am using the following ugly code:

	while (cp < end && triples < MINSKIP) {
		if ((*cp | *(cp+1) | *(cp+2)) == 0) ++triples;
		else triples = 0;
		cp += 3;
	}



More information about the Comp.lang.c mailing list