gpt4 book ai didi

c++ - 哪些工具可以报告此类未初始化变量的使用情况?

转载 作者:行者123 更新时间:2023-11-30 03:45:52 29 4
gpt4 key购买 nike

考虑下面的程序

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=undefinedvalgrind .但是这些工具不会向我报告 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com