Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does g++ warning about uninitialized variable depend on the type of the variable? (it warns for an int but not for a double)

Tags:

c++

g++

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

like image 438
Somebody Avatar asked Feb 14 '11 19:02

Somebody


People also ask

What does it mean when a variable is uninitialized?

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.

What happens when you try to use an uninitialized variable?

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.

Why are uninitialized variables bad?

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.

What does uninitialized variable mean in C++?

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.


3 Answers

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.

like image 168
Gene Bushuyev Avatar answered Oct 01 '22 18:10

Gene Bushuyev


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)

like image 41
Dan Avatar answered Oct 01 '22 18:10

Dan


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.

like image 34
frankc Avatar answered Oct 01 '22 20:10

frankc