This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: conditional assigments vs. "may be used uninitialized"


2008/10/22 Hallvard B Furuseth <h.b.furuseth@usit.uio.no>:
> Info node (gcc)Warning Options mentions that gcc warns about
>
>            int save_y;
>            if (change_y) save_y = y, y = new_y;
>            ...
>            if (change_y) y = save_y;
>
> However that's not always true, so it looks like gcc does have
> the smarts to drop the warning.  Could that be improved?

In the case of Wuninitialized warnings, if GCC gets the answer right,
it may not get the answer by the same reasoning as you. A notable
example is the following code:

   1 sub()
   2 {
   3   int i = 0;
   4   int j = 0;
   5   int k;
   6
   7   while ((i | j) == 0)
   8     {
   9       k = 10;
  10       i = sub ();
  11     }
  12
  13   return k;
  14 }


Here GCC does not warn for line 13. But GCC never knows that the loop
is executed at least once. In fact what happens is that GCC assumes
that the uninitialized value of k is 10 and hence it propagates this
value so line 13 becomes return 10. If you substitute 0 by a variable
in line 7 there is still no warning.

If you are interested in improving uninitialized warnings I would
suggest to take a look to
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

(It is perhaps a bit outdated, since a few things have improved in GCC 4.4).

Cheers,

Manuel.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]