Dubious "'foo' might be used uninitialized in this function" message

E. Weddington ericw@evcohs.com
Tue Dec 14 16:40:00 GMT 2004


Dmitry Antipov wrote:

> When compiling the following program,
>
> #include <unistd.h>
>
> int f (int x, int y)
> {
>  int z;
>
>  if (x)
>    z = getppid ();
>  y = getpid ();
>  if (x)
>    y += z;
>  return x + y + z;
> }
>
> GCC (with '-Wall') always says:
>
> w.c: In function `f':
> w.c:5: warning: 'z' might be used uninitialized in this function
>
> which is not true.
>
> Here 'z' is initialized under 'if (x)' condition, and 'z' always used 
> under
> 'if (x)' condition. Also, it's clear that 'x' isn't accessed between 
> 'if (x)',
> so it's impossible to access uninitialized 'z'.
>
> Is it reasonable to learn GCC do more analysis in attempt to avoid
> warning in this case ? How is it complex ?
>

Even if you rearranged the code to remove your warning above:

int f (int x, int y)
{
   int z;

   y = getpid ();
   if (x)
  {
      z = getppid ();
      y += z;
   }

   return x + y + z;
}

You'll still get an uninitialized variable warning for your return 
statement, where you do use z uninitialized.



More information about the Gcc mailing list