Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make g++ warn on uninitialized POD member variable

Tags:

c++

warnings

g++

Is there a way to print a warning if you forget to declare in ctor initialization list a member POD? I'm looking through docs and can't find anything. g++-4.4 here.

like image 645
piotr Avatar asked Jul 20 '11 18:07

piotr


People also ask

How do you warn about uninitialized variables in C++?

If you want to warn about code that uses the uninitialized value of the variable in its own initializer, use the -Winit-self option. These warnings occur for individual uninitialized elements of structure, union or array variables as well as for variables that are uninitialized as a whole.

What is uninitialized warning in C++?

Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option. This warning is enabled by -Wall in C++. This option controls warnings when a declaration does not specify a type.

How to suppress the-wunused warning in GCC?

These functions changed semantics in GCC 4.4. Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration). To suppress this warning use the unused attribute (see Variable Attributes ). This warning is also enabled by -Wunused together with -Wextra .

Why does GCC issue warnings when I use a common variable?

To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe. See Common Variable Attributes . Warn for cases where adding an attribute may be beneficial.


2 Answers

You can enable -Weffc++ to get a heap of ridiculous warnings, including every single non-ctor-initialized member.

To check for actual UB, use valgrind.

like image 92
Kerrek SB Avatar answered Oct 20 '22 08:10

Kerrek SB


You can use the option -Wuninitialized (and also -Wall).
However, It only warns if the Uninitialized type is being used.

Also, Ofcourse you can use other softwares like Valgrind and Cppcheck to determine Uninitialized types.

like image 25
Alok Save Avatar answered Oct 20 '22 09:10

Alok Save