Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!uunet!uunet!zephyr.ens.tek.com!tektronix!percy!m2xenix!quagga!ucthpx!uctcs.uucp!gram From: gram@uctcs.uucp (Graham Wheeler) Newsgroups: comp.lang.c Subject: What does ANSI C say about short circuit evaluation? Message-ID: <1991May22.092404.25297@ucthpx.uct.ac.za> Date: 22 May 91 09:24:04 GMT Sender: news@ucthpx.uct.ac.za (UCT News Admin.) Reply-To: gram@uctcs.uucp (Graham Wheeler) Organization: Dept. of Computer Science, University of Cape Town Lines: 31 The following question has caused a bit of debate here in the last couple of days. There are two parts: i) Does ANSI C say that compilers can rearrange the order of expression evaluation? ii) Does it say that Boolean expressions must be evaluated with short- circuit evaluation? We think the answers to both of these are yes, but we aren't sure. This would mean that a statement like: if (ptr && ptr->next) ... could potentially cause a segmentation violation on a system with meory protection if the compiler was stupid enough to try to evaluate the second (more complex) subexpression first - ie, one cannot rely on short-circuit evaluation to prevent this from happening. This doesn't bother me much as I tend to write such pointer expressions as if (ptr) if (ptr->next) ... for safety, but it may cause some elusive buggy behaviour. Preferably e-mail your responses, thanks. Graham Wheeler | "That which is weak conquers the strong, Data Network Architectures Lab | that which is soft conquers the hard. Dept. of Computer Science | All men know this; none practise it" University of Cape Town | Lao Tzu - Tao Te Ching Ch.78