Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!rex!uflorida!haven!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Is something wrong with the compiler ? Message-ID: <26875@mimsy.umd.edu> Date: 6 Oct 90 04:06:03 GMT References: <1895@tuvie> <645@demott.COM> <1903@tuvie> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 43 In article <1903@tuvie> hp@vmars.tuwien.ac.at (Peter Holzer) writes: >Consider ... a = (a = a + 1) + 1 >This will compile (unoptimized) into: > >tmp1 = a + 1 >a = tmp1 * >tmp2 = tmp1 + 1 >a = tmp2 * > >where the lines marked with * may or may not be deferred until the next >sequence point.... [Second example deleted.] ... Any further comments? Yes. The ANSI standard appears% to allow the compiler to compile it as tmp0 = a tmp1 = tmp0 + 1 a = rand() * tmp2 = tmp0 + 2 a = tmp2 * (using the same notation as above). If the store of tmp2 happens before the store of tmp1, a could wind up with a random value. For a more realistic example, consider a machine that can do several operations at once. This machine might issue two separate `simultaneous' operations, those being `increment a' and `set a=a+2'. This could fetch the value of `a' after the increment has changed only part of the ultimate value (assume it is a parallel machine with lots of one-bit CPUs). The end result is that dreaded word, `undefined'. In short, when the ANSI standard says that the result of an operation is undefined, it means UNDEFINED. The computer can do anything (like turn into a flower)---the system does not have to do anything remotely reasonable. When it says a result is implementation-defined, the system can still do anything, but the product description has to tell you what it does. ----- % Note weasel word. :-) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris