C compiler warning messages?

johnr@ims.com (John Roberts)
6 Apr 1997 22:30:14 -0400

          From comp.compilers

Related articles
C compiler warning messages? johnr@ims.com (1997-04-06)
Re: C compiler warning messages? trt@duke.cs.duke.edu (1997-04-07)
Proactive Compiler Error Messages cdg@nullstone.com (Christopher Glaeser) (1997-04-08)
Re: C compiler warning messages? morris@CAM.ORG (1997-04-08)
Re: C compiler warning messages? chase@naturalbridge.com (David Chase) (1997-04-11)
Re: C compiler warning messages? joshua@intrinsa.com (1997-04-16)
Re: C compiler warning messages? morris@CAM.ORG (Morris Bernstein) (1997-04-16)
[1 later articles]
| List of all articles for this month |

From: johnr@ims.com (John Roberts)
Newsgroups: comp.compilers
Date: 6 Apr 1997 22:30:14 -0400
Organization: Integrated Measurement Systems, Inc.
Keywords: storage, errors, practice

Hi,


I ran into a situation in some C code where I think the compiler
could've helped me out with some intelligent warning messages, but
didn't.


I'm running under Solaris 2.5 using the stock Sun compilers.


The situation was a function like this:


char *fun()
{
char rstr[20];
...
...
strcpy(rstr, "1");
...
...
return (char *)&rstr[0];
}


(Fortunately I didn't write this code!)


Anyway the problem with the above fragment is that rstr[] is on the
stack and can get reclaimed at any time. This was generating
intermittent errors during execution of the program, sometimes fun()
would return the correct string, sometimes not.


The solution I used was to declare char rstr[20] using "static", which
solved the problem.


However, in general, it seems to me that the C compiler should of
given me a warning about returning a pointer to a string about to go
out of scope.


Anyone else out there have any experience with warning messages? Can
compilers detect this situation? Are there any other compilers
(e.g. GNU) that would give a warning on this?


Thanks!


John Roberts
johnr@ims.com
http://www.teleport.com/~johnro
[GCC 2.x does indeed produce a warning. Do compilers commonly diagnose
this bug? And remember, with a proper garbage collected storage model,
this wouldn't be a bug! -John]




--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.