Re: C compiler warning messages?

morris@CAM.ORG (Morris Bernstein)
8 Apr 1997 09:41:54 -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)
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)
Re: C compiler warning messages? oz@ds9.rnd.border.com (1997-04-18)
| List of all articles for this month |

From: morris@CAM.ORG (Morris Bernstein)
Newsgroups: comp.compilers
Date: 8 Apr 1997 09:41:54 -0400
Organization: Communications Accessibles Montreal, Quebec Canada
References: 97-04-042
Keywords: storage, errors, practice, comment

John Roberts <johnr@ims.com> wrote:
...
>The situation was a function like this:
>
> char *fun()
> {
> char rstr[20];
> return (char *)&rstr[0];
> }
...


>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.


Certainly, a compiler could generate a warning for your particular
example by treating the return statement as a special case.
Unfortunately, there's no general way to solve the problem without
generating many spurious warning messages.


Consider:
...
if( some_test() ) {
pstr = &stack_array[0];
} else {
pstr = malloc(42);
}
At the end of the conditional, all the compiler can determine is that
pstr *might* point to stack-allocated memory. Even more difficult:
pstr = foo(&stack_array[0]);
To detect the *potential* error, a compiler has to be able to solve
the interprocederal points-to problem, which is highly nontrivial.


In general, the kind of defect detection you're interested in is very
difficult to do at compile time. You're much better off using a C
interpreter like Saber C (are they still around?). The interpreter
can much more easily check such conditions at run time.




Morris
--
Morris Bernstein morris@cam.org
[It's certainly useful to warn about returning a direct reference to an auto
variable, which is an error 99+% of the time. Saber C is indeed still around,
now it's called Code Center and the vendor is called Centerline. -John]
--


Post a followup to this message

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