Date: Sun, 15 Dec 1996 09:56:12 -0500 Message-Id: <199612151456.JAA24072@delorie.com> From: DJ Delorie To: max AT alcyone DOT com CC: djgpp AT delorie DOT com In-reply-to: <32B3753E.26758647@alcyone.com> (message from Erik Max Francis on Sat, 14 Dec 1996 19:49:18 -0800) Subject: Re: strange warning Erik Max Francis 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.