extra c++ warnings

George greerga@entropy.muc.muohio.edu
Tue Dec 30 12:07:00 GMT 1997


The C++ end of egcs (and gcc 2.7.2.3) gives warnings on && constructs that
the C portion does not.

Example:

greerga@bacon:~/c$ cat warning.cpp
#include <stdio.h>
#include <stdlib.h>

int return_0(void) { return 0; }

int main(void)
{
  int j;

  if (return_0() == 0 && (j = return_0()) == 0)
    printf("%d\n", j);

  return 0;
}
greerga@bacon:~/c$ egcs++ -O2 -Wall warning.cpp
warning.cpp: In function `int main()':
warning.cpp:8: warning: `int j' might be used uninitialized in this function
greerga@bacon:~/c$ mv warning.cpp warning.c
greerga@bacon:~/c$ egcs -O2 -Wall warning.c
greerga@bacon:~/c$

It doesn't realize that in order to get to the printf, the assignment must
happen.

However, the following works without error:

greerga@bacon:~/c$ cat warning.cpp
#include <stdio.h>
#include <stdlib.h>

int return_0(void) { return 0; }

int main(void)
{
  int j;

  if (0 == 0 && (j = return_0()) == 0)
    printf("%d\n", j);

  return 0;
}
greerga@bacon:~/c$ egcs++ -O2 -Wall warning.cpp
greerga@bacon:~/c$

It seems to only happen when the first part calls a function (a comparison
against a variable, such as 'i == 4' does not provoke the warning either)

-George Greer




More information about the Gcc mailing list