This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions
- To: <chitty at libero dot it>
- Subject: Re: c/4101: gcc 2.95.4 compiles bad code with nested static functions
- From: Bernd Schmidt <bernds at redhat dot com>
- Date: Thu, 23 Aug 2001 20:15:16 +0100 (BST)
- cc: <gcc-gnats at gcc dot gnu dot org>, <alessandro_amici at telespazio dot it>, <nobody at gcc dot gnu dot org>, <gcc-prs at gcc dot gnu dot org>, <gcc-bugs at gcc dot gnu dot org>
(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