Easy way find uninitialized member variables

后端 未结 11 928
时光说笑
时光说笑 2020-11-29 22:20

I am looking for an easy way to find uninitialized class member variables.

Finding them in either runtime or compile time is OK.

Currently

相关标签:
11条回答
  • 2020-11-29 22:36

    Visual Studio (MSVC) has a /sdl (Enable Additional Security Checks) compiler option (http://msdn.microsoft.com/en-us/library/jj161081.aspx). At run-time, it:

    Performs class member initialization. Automatically initializes class members of pointer type to zero on object instantiation (before the constructor runs). This helps to prevent use of uninitialized data associated with class members that the constructor does not explicitly initialize.

    This will not help you detect uninitialized member variables at compile-time, but it makes the behaviour more predictable when it happens at run-time. You shouldn't write code that relies on this option being enabled though, of course.

    0 讨论(0)
  • 2020-11-29 22:39

    Clang with clang-analyze is able to do this. It will event create a nice HTML report that indicates when the unused variable is accessed.

    0 讨论(0)
  • 2020-11-29 22:46

    Valgrind (FREE, on Linux) and Purify (on Windows) find un-initialized variables, invalid pointers and such by running your code in a special virtual machine.

    This is easy to use and extremely powerful; it will likely find many bugs beyond the obvious un-initialized variables.

    Coverity, Klocwork and Lint can find un-initialized variables using static code analysis.

    0 讨论(0)
  • 2020-11-29 22:46

    /analyze on Visual Studio ("Team System")

    0 讨论(0)
  • 2020-11-29 22:52

    cppcheck will find this, e.g.:

    cppcheck my_src_dir --output-file=check.txt --inconclusive --enable=warning
    
    0 讨论(0)
  • 2020-11-29 22:53

    Valgrind can tell you if you are on linux.

    0 讨论(0)
提交回复
热议问题