Mail Archives: djgpp/1996/12/15/10:01:16
Erik Max Francis <max AT alcyone DOT com> wrote:
> Alaric Dailey wrote:
> > I am trying to create a make file for a package of source code written in
> > ansi-C everything seems to compile ok except for one code fragment. see
> > below. this code always produces this warning
> >
> > context.c: In function `go_deep':
> > context.c:34 warning: function returns address of local variable
>
> This is inherently a bad thing. Perhaps the authors are trying to do
> something tricky based on the knowledge that in the limited circumstances
> this is to be used everything will be okay, but that seems unlikely in a
> preemptive multitasking system.
>
> Basically, the problem is that an automatic variable is destroyed once the
> block it is declared in is left. Returning the address of an automatic
> variable is a Bad Thing To Do -- it means that you're pointing to an area of
> the stack which may be reclaimed at any time.
More likely, the author intended exactly what the warning implies.
The program is probably calling a few functions and checking to see
how much stack is being used (it doesn't ever use dereference the
pointer, just subtracts it from something else) so that enough stack
can be allocated (and the right end can be pointed to) for the
separate threads.
In this case the warning is just that - a warning. The compiler
thought you *might* be doing something wrong, but you aren't, so
ignore it.
If you really want to get rid of the warning, cast the address to an
(int) before returning it. Then the compiler realizes that it's the
value of the address you want, not the memory it points to.
- Raw text -