From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bernd Schmidt To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org Subject: Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions Date: Thu, 23 Aug 2001 12:16:00 -0000 Message-id: <20010823191603.21714.qmail@sourceware.cygnus.com> X-SW-Source: 2001-08/msg00600.html List-Id: The following reply was made to PR c/4101; it has been noted by GNATS. From: Bernd Schmidt To: Cc: , , , , Subject: Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions Date: Thu, 23 Aug 2001 20:15:16 +0100 (BST) (I currently can't access the gnatsweb page for some reason, otherwise I'd have closed this. If gnats works for someone, please close...). > /* this must do a certain thing only the 1st time, > but we don't want a check as if (first_time) {do}, > so we use a function pointer that the first time > does, the others just returns. > */ > void loop(void) > { > void f1(void); > void f2(void); > static void (*f)(void) = f1; > > void f2(void) { } /* this is empty */ > > void f1(void) > { > /* ... do something */ > f=f2; /* the problem is HERE */ > } > > (*f)(); /* do something only the first time */ > } This isn't actually valid. The line > static void (*f)(void) = f1; constructs a trampoline on the current stack of the function loop, and once you've exited from that function, the contents of the stack are no longer valid. Basically, f1 is treated as a local variable, and you can't keep pointers to those across multiple calls to a function either. Bernd