I'm currently trying to understand in which cases g++ warns about uninitialized variables. Consider the following piece of code:
#include <iostream>
typedef double barType;
struct foo {
barType bar;
};
int main() {
foo f;
std::cout << f.bar << std::endl;
}
If I compile it like this I get no warning:
$ g++ -O1 -Wall test.cc -o test
but if I change barType to int:
$ g++ -O1 -Wall test.cc -o test
test.cc: In function ‘int main()’:
test.cc:17: warning: ‘f.foo::bar’ is used uninitialized in this function
How can the warning depend on the type? It is uninitialized in both cases.
I'm using:
$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Thanks,
Somebody
In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.
An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.
An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using. This can lead to errors that are very hard to detect since the variable's value is effectively random, different values cause different errors or none at all.
In C and C++, local variables aren't initialized by default. Uninitialized variables can contain any value, and their use leads to undefined behavior. Warning C4700 almost always indicates a bug that can cause unpredictable results or crashes in your program.
It's undefined behavior, which is not required to be diagnosed, so the compiler is free to make its judgement. They could have done better.
At a guess they might be more concerned with integral types being used uninitialized than say a float or a double since you could use integral types with pointer offsets without casting which could be very bad(tm)
You can get this to warn if you are compiling with -O. I'm not really clear on why but if I had to guess it is for compilation speed purposes, i.e. it already need to figure this out for optimization so will only report if when you want to optimize.
There is also -Wuninitialized, which is actually not included in "all", but that that also requires -O anyway. At least if you do -Wuninitialized, the compiler will warn you that it can't warn you...
cc1plus: warning: -Wuninitialized is not supported without -O
One good takeaway from this is that -Wall is poorly named. There are other -W options not included in "all". Consult the docs for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With