Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!orion.oac.uci.edu!uci-ics!rfg From: rfg@ics.uci.edu (Ronald Guilmette) Newsgroups: comp.lang.c++ Subject: Re: subtle inlining/sequence points bug in CFront 2.0 Keywords: inline, CFront, bug Message-ID: <26422837.21835@paris.ics.uci.edu> Date: 5 May 90 01:11:19 GMT References: <8066@goofy.Apple.COM> Reply-To: rfg@ics.uci.edu (Ronald Guilmette) Organization: UC Irvine Department of ICS Lines: 57 In article <8066@goofy.Apple.COM> scott@goofy.apple.com (scott douglass) writes: >I've found, actually I have been burned by, a subtle bug involving >inlining and sequence points in CFront 2.0. In some cases CFront >accidentally eliminates the sequence point before the call of an >inline function. I believe that you have misunderstood the exact definition of the term "sequence point" (at least as it is proscribed by the ANSI C standard). Reading sections A.2 and 3.6 of the ANSI C standard may help. > >Example: > > class A > { > public: > A* next; > int i; > A* get_next(); > int f(); > }; > > A* A::get_next() { return next; } > > void g(A* a) > { > int b = ( a->get_next()->i == a->f() ); > } > >Assume that A::f() may change A::next which CFront must since A::f() >if not a const member. Since order of evaluation is not defined for >expressions either a->get_next() or a->f() will happen first, but >the "->i" will definetly refer to one of two possible As. > >Now the problem. If I make A::get_next() inline CFront generates >code something like the following: > > b = ( a->next->i == f(a) ); > >The semantics of this expression are not defined. Your C compiler >may, and mine did, evaluate a->next into a temporary, evaluate a->f() >which may change a->next, and finally evaluate temporary->i which >may no longer be valid. It seems that you may have desired that the entire left hand operand of == be evaluated before *any* of the right hand operand of the same == was evaluated. While this may indeed have been desirable in this case, I don't see that this is required by either the ANSI C rules or the (virtually identical?) C++ rules relating to sequence points. I'd be perfectly happy to be proven wrong however. // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.