Where are my compiler warnings?
I've had to fix several bugs caused by variables not being initialized, so it would be nice if the compiler could issue warnings about that. Normally, it does:
Code:
int main(int argc, char *[])
{
char *ptr;
char *ptr2;
if (argc==10) ptr="hello";
return int(*ptr + *ptr2);
}
>g++ -v
...
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)
>g++ -O2 -Wall -Wextra -Wuninitialized -o dum dum.cc
dum.cc: In function ‘int main(int, char**)’:
dum.cc:6: warning: deprecated conversion from string constant to ‘char*’
dum.cc:7: warning: ‘ptr2’ is used uninitialized in this function
dum.cc:4: warning: ‘ptr’ may be used uninitialized in this function
>
All 3 warnings makes sense: a string constant has type const char * while ptr is char *, the code does not even try to initialize ptr2 and ptr is only maybe initialized.
The command-line options given to g++ ensure that all warnings are enabled and that optimization is run (which is necessary to detect som types of warnings).
Let's try to fix the first warning by changing the type of "ptr":
Code:
int main(int argc, char *[])
{
const char *ptr;
char *ptr2;
if (argc==10) ptr="hello";
return int(*ptr + *ptr2);
}
>g++ -O2 -Wall -Wextra -Wuninitialized -o dum dum.cc
dum.cc: In function ‘int main(int, char**)’:
dum.cc:7: warning: ‘ptr2’ is used uninitialized in this function
>
What happened to the warning about ptr perhaps being used uninitialized?
The problem is not limited to pointers either. This version:
Code:
int main(int argc, char **)
{
int foo;
if (argc == 10) {
foo = 4711;
}
return foo;
}
gives no warnings at all. I'm suspecting a compiler bug, but would like to know what you guys think before I report it.
Re: Where are my compiler warnings?
Weird. I compiled the code on gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) and got:
test.cc: In function `int main(int, char**)':
test.cc:3: warning: `const char*ptr' might be used uninitialized in this
function
test.cc:4: warning: `char*ptr2' might be used uninitialized in this function
Don't have your version installed though, so it may be release-specific. However, my guess is that the compiler has optimized out the const char* ptr reference, and thus does not report it.
Re: Where are my compiler warnings?
I've tried to check what the "const ptr" points to when no codeline assigns a value to it. It seems to point to the "hello" string anyway. Does g++ assign uninitialized "const char *"s to the first string in the data section or something?
I guess I'd better file a bug-report.
Re: Where are my compiler warnings?
Try compiling without the -O2 (which performs level 2 optimizations). It sounds like the code is just being optimized out.
Re: Where are my compiler warnings?
According to the g++ man page, optimization is required for the compiler to be able to notice uninitialized variables.
I tried compiling with -O1 and without any -O, in neither case do I get a warning about "ptr may be used initialized".
Re: Where are my compiler warnings?
There seems to be a lively discussion about how to handle uninitialized variables: http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings