How to find and avoid uninitialized primitive members in C ++?

There is a general C ++ error with uninitialized primitive elements:

#include <iostream> class A { public: int x; }; int main() { A a; std::cout << ax; return 0; } 

ax will be uninitialized. I understand why this is happening, and you want to find a solution to search for such errors. I checked gcc and cppcheck, they do not report these members.

EDIT Checked by gcc with flags -Wall -Wextra -Werror -pedantic -Wold-style-cast -Wconversion -Wsign-conversion -Wunreachable-code

The first version of gcc that detects an error is 5.1. g ++ - 4.9 does not detect it, clang ++ - 3.6 does not work either.

+5
source share
2 answers

Yes they do & hellip; view:

 main.cpp: In function 'int main()': main.cpp:10:18: warning: 'aA::x' is used uninitialized in this function [-Wuninitialized] std::cout << ax; ^ 0 

In the above example, I am using the GCC 5.1 -Wall with -Wall .

Include additional GCC alerts and / or updates.

It has also been verified and found that it should not be warned:

  • GCC 4.4.7
  • GCC 4.9.2 ( RiaD )
  • Clang 3.6.0

Honestly, I'm not sure what else you can do. You can create a tool for this, but then you must create a compiler or static analyzer. :)

So, I think, I just hope that people who know how to do this catch up with & hellip;

+6
source

This error is diagnosed using valgrind using the (default) memcheck , generating a series of warnings, including:

 $ valgrind ./unin … ==12185== Use of uninitialised value of size 8 ==12185== at 0x4F39BC3: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==12185== by 0x4F3AD89: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==12185== by 0x4F3AF8C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==12185== by 0x4F474E9: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==12185== by 0x400763: main (in [censored]/unin) 

It must also be found with Clang's location disinfectant. However, I understand that you are most interested in checking compile time. In the end, existing test suites can never execute any code, and catching an error is always better before. You can use GCC 5.1 (even if you used it only for this purpose), or you can use a dedicated static analyzer. Fortunately, clang comes with a static analyzer called as scan-build (included at least in Debian / Ubuntu packages):

 $ scan-build clang -c unin.cxx scan-build: Using '/usr/lib/llvm-3.6/bin/clang' for static analysis unin.cxx:10:3: warning: Function call argument is an uninitialized value std::cout << ax; ^~~~~~~~~~~~~~~~ 1 warning generated. scan-build: 1 bug found. 
+1
source

All Articles