c/4101: gcc 2.95.4 compiles bad code with nested static functions

Bernd Schmidt bernds@redhat.com
Thu Aug 23 12:16:00 GMT 2001


The following reply was made to PR c/4101; it has been noted by GNATS.

From: Bernd Schmidt <bernds@redhat.com>
To: <chitty@libero.it>
Cc: <gcc-gnats@gcc.gnu.org>, <alessandro_amici@telespazio.it>,
   <nobody@gcc.gnu.org>, <gcc-prs@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org>
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
 



More information about the Gcc-prs mailing list