考虑下面的程序
struct X
{
int d;
X(){} // forget to initialize d
};
int main()
{
X x;
volatile int y=x.d; // read uninitialized d
(void)y; // (kill useless warning)
}
显然它有未定义的行为,我通常希望能够使用一些调试工具来捕捉。我试过 cppcheck(使用他们的 online demo )和 g++ -fsanitize=address
以及 g++ -fsanitize=undefined
和 valgrind
.但是这些工具不会向我报告 x.d
在读取时未初始化。
我可以使用什么工具来捕捉这些未定义的行为(最好在 Linux 上运行)?
最佳答案
扫描提供的例子
$ more test.cpp
struct X
{
int d;
X(){} // forget to initialize d
};
int main()
{
X x;
volatile int y=x.d; // read uninitialized d
(void)y; // (kill useless warning)
}
使用最新版本的 cppcheck(版本 1.72 dev)给出:
$ cppcheck --enable=all test.cpp
Checking test.cpp...
[test.cpp:4]: (warning) Member variable 'X::d' is not initialized in the constructor.
关于c++ - 哪些工具可以报告此类未初始化变量的使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478831/