Hi all,

I am somewhat confused about the status of the 
"may be used uninitialized" warning...

Consider:
--- testit.c ---
#include <stdio.h>

static void
testit(int *a, int cnt)
{
  struct {int score; int d;} best;
  int i;
  best.score = 0;
  for (i = 0; i < cnt; i++)
    if (a[i] > best.score) {
      best.score = a[i];
      best.d = i;
    }
  if (best.score <= 0 || best.d > 8)
    return;
  printf("Found best %d at %d\n", best.score, best.d);
}

int
main()
{
  int a[10];
  testit(a, 10);
  return 0;
}
---

This used to compile without warning using: gcc -Wall -O -o testit testit.c
(tested with gcc version 3.4.2)

On my FC4 box with gcc version 4.0.2 20051125 (Red Hat 4.0.2-8) it says:
testit.c: In function 'testit':
testit.c:6: warning: 'best.d' may be used uninitialized in this function

I poked a bit in bugzilla, but it spewed 200 bugs related to the warning and 
I'm not too sure I can grok a definitive answer from there.

I used to go by the assumption that a way to avoid an uninitialized warning 
was to group related variables in a struct, and as long as some of the struct 
was initialized, things would be fine.  It seems to no longer be the case.

Also, I can't find a way to execute the code and use best.d uninitialized.

I've seen in a BZ comment that one way to kill the warning is to write:
  struct {int score; int d;} best = best;
That indeed does work, but looks a bit strange.  And making this a rule will 
quickly kill the usefullness of such a warning...

Is there any word of wisdom I'm missing here ?

Thanks for your time.

Cheers,
                                        Christian


Reply via email to