Ok, a slightly simpler testcase still shows the warning:

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

static void
testit(unsigned int *a, unsigned int cnt)
{
  struct {unsigned int score; unsigned int d;} best;
  unsigned 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)
    return;
  printf("Found best %d at %d\n", best.score, best.d);
}

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

Yes, I'm sure best.d can't be read in any way unless it's been set 
before.  I understand it is hard for the compiler to infer that fact.

But... it used to be the case that the compiler didn't try to warn about 
uninitialized variables embedded in structs (or so I seem to remember...)
So I was wondering if this was some kind of regression...

Of course, initializing best.d is an easy way out, but it's sub-optimal and
useless.

The "struct {unsigned int score; unsigned int d;} best = best;" trick seems to
work fine and has no influence on the generated code.  I guess I merely wanted
some confirmation that it was the only optimal (and maybe recommended) way out
(unless the compiler can be made smarter about this somehow)

Thanks for your answers.

Cheers,
                                        Christian


Reply via email to