Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!rutgers!columbia!garfield!eppstein From: eppstein@garfield (David Eppstein) Newsgroups: comp.lang.c++ Subject: A bug in inline functions Message-ID: <5977@columbia.edu> Date: 1 Nov 88 21:50:48 GMT References: <191@imspw6.UUCP> Sender: news@columbia.edu Organization: Columbia University CS Department Lines: 44 This is in #ident "@(#)cfront:CC 1.11". The problem: CC neglects to rename the arguments of an inline method, creating a possible collision with global variables changed in the method. The following example illustrates this. The function x->pop() changes a global variable s, then references x->val. If called as s->pop(), the generated code incorrectly references s->val for the new value of s (which may be zero giving strange results) rather than the old value (which should still be pointed to by this). If I make the method non-inline, it works correctly. Correct output Output with inline bug 2 1 1 0 0 1125191424 ------------ #include class stack * s = 0; class stack { int val; stack * next; public: stack(int v) { val = v; next = s; } void pop() { s = s->next; // change stack top cout << val << "\n"; // print value of THIS (not stack top) } }; main() { s = new stack(0); s = new stack(1); s = new stack(2); s->pop(); s->pop(); s->pop(); } -- David Eppstein eppstein@garfield.cs.columbia.edu Columbia U. Computer Science