This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


(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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]