为什么编译时会出现如下错误

发布于 2024-10-20 19:11:37 字数 289 浏览 7 评论 0原文

作为我的程序的一部分,我使用了以下代码:(

///////////////
98:::printf("%d",abc->stv)
//////////////
100::if(abc)
//////////////

产生了以下错误)

Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 100

As part of my program, I used following code:

///////////////
98:::printf("%d",abc->stv)
//////////////
100::if(abc)
//////////////

(the following error was produced)

Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 100

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪也成诗 2024-10-27 19:11:37

if (abc) 测试 abc 是否为空指针。

编译器警告您,您已经假设 abc 不是空指针(通过在第 98 行取消引用它),这意味着

  • if (abc) 测试是多余的(因为它永远不会为真),或者
  • 第 98 行对 abc 的取消引用可能是不正确的,因为 abc 实际上可能为 null。

if (abc) tests whether abc is a null pointer or not.

The compiler is warning you that you've already assumed that abc is not a null pointer (by dereferencing it on line 98), which means either

  • the if (abc) test is redundant (because it will never be true) or
  • the dereferencing of abc on line 98 is potentially incorrect because abc might in fact be null.
撩发小公举 2024-10-27 19:11:37

如果您测试 abc ,则对编译器来说意味着它可能为 null。因此像abc->stv那样取消引用指针是一个可能的错误。解决方案是将 printf 代码包含在 if 块内:

if(abc)
{
    printf("%d",abc->stv)
    ...
}

If you test for abc it means to the compiler it might be null. Therefore dereferencing the pointer like in abc->stv is a possible error. A solution is to enclose the printf code inside the ifblock:

if(abc)
{
    printf("%d",abc->stv)
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文