1

Is there a warning flag so that gcc will warn about every variable that is defined and not immediately initialized? I know about -Wuninitialized but it does not warn about every case. Consider this code:

int foo(int i)
{
  int a;
  a= i%2 ? 0x42 : 42;
  return a;
}

Even tho this is perfectly valid code and does not cause UB, i would like a warning for this. The line int a; should be replaced by int a=0;. Can i tell gcc to warn about int a;?

Edit, a more complex example:

#include <stdio.h>

int foo(void)
{
  int a;
  if(1==scanf("%i",&a))
  {
    return a;
  }
  return 0;
}

Here i want gcc to warn about int a;

  • Does this answer your question? [GCC -Wuninitialized / -Wmaybe-uninitialized issues](https://stackoverflow.com/questions/14132898/gcc-wuninitialized-wmaybe-uninitialized-issues) – Krishna Kanth Yenumula Dec 24 '20 at 18:51
  • @EricPostpischil I tried it with `-Wall -Wextra -Wpedantic -Wunused -Wuninitialized`, gcc does still not warn. – 12431234123412341234123 Dec 24 '20 at 18:53
  • @KrishnaKanthYenumula No. The linked question talks about initializing a variable in a `if` body or somewhere else where the PC may or may not land. I talk about initializing it in the same statement as the definition. – 12431234123412341234123 Dec 24 '20 at 18:55
  • @EricPostpischil No. There is also the `-Wswitch-default` which is not enabled by any of`-Wall -Wextra -Wpedantic -Wunused -Wuninitialized`. – 12431234123412341234123 Dec 24 '20 at 18:58

2 Answers2

1

This does not seem a language problem, so I don't think GCC will ever react; actually, it is defining a value for a that might be warned against by some linter:

 int a = 0;
         ^-- warning: redundant initialisation (value is immediately discarded)
 a= i%2 ? 0x42 : 42;

What you can, perhaps, do - but this will not catch all possible cases - is recognize the issue at the static level.

I myself do this for some specific cases (which is why I have trained myself to write code in what is sometimes a unusual way -- for example, free(p); p = NULL; on a single line, or if p is to be immediately discarded, then free(p); // p = NULL;, so no pointer can ever point to freed memory, and I track this using grep. The same with fclose(fp); fp = NULL).

In this case, at least for the common data types and simple initializations, you can recognize the unassignment with a regex too:

((unsigned\\s+)?(long\\s+)?(int|char|float|double)...

(Not a real regex, just throwing in tokens at random. You may want to look at other questions to this effect).

Once you have a means of singling out all definitions, any definition that does not include an equal sign is suspect and can be printed for further analysis. You can avoid a "unneeded initialization" by writing int a; //OK and ignore the lines containing '//OK'.

LSerni
  • 55,617
  • 10
  • 65
  • 107
0

In your code sample, being warned to replace the int a; line with int a = 0; will be counter-productive. A good compiler would/should warn you about the latter, as there you are assigning a value to a that will never be used (the value is immediately overwritten in the following line of code).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I see that other people may like other patterns more than the patterns i like. But i want to initialize every variable in the same statement as i define them. I updated the question and added a more complex example. – 12431234123412341234123 Dec 24 '20 at 19:28
  • @Cheatah I agree. It is only when an uninitialized variable is actually *used* that a compiler (or even a linter) should warn. – Adrian Mole Dec 24 '20 at 23:36