当 0  作为参数传递时,GCC 可以发出警告吗?

发布于 2025-01-06 21:47:45 字数 95 浏览 1 评论 0原文

有人告诉我,您可以在代码中添加一些特殊指令,以使 GCC 在检测到 0 作为参数传递时(这意味着在编译时可能)发出警告。

我已经找过了,但没能找到。这是真的吗?

I have been told that you could add some special instruction to your code to make GCC issue a warning when it detects that 0 is being passed as an argument (which means, when it is possible at compile-time).

I have looked for it but haven’t been able to find it. Is this true?

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

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

发布评论

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

评论(1

绿萝 2025-01-13 21:47:45

有一个 函数属性 您可以使用它来警告空指针:

void foo(void *data) __attribute__((nonnull));

int main(void)
{
    foo(0);
    return 0;
}
$ gcc -Wall -c t.c
t.c: In function ‘main’:
t.c:5:5: warning: null argument where non-null required (argument 1) [-Wnonnull]

I不过,我不知道有任何内置的东西可以检查整数类型是否为 0。

不过,您可能会在 Linux 内核的各种 BUILD_BUG_* 宏中找到适合您需要的内容。它们位于 include/linux/kernel.h 中。 (此处交叉引用。)

There is a function attribute you can use to warn on null pointers:

void foo(void *data) __attribute__((nonnull));

int main(void)
{
    foo(0);
    return 0;
}
$ gcc -Wall -c t.c
t.c: In function ‘main’:
t.c:5:5: warning: null argument where non-null required (argument 1) [-Wnonnull]

I'm not aware of anything built-in to check for 0 for integer types though.

You might find something that suits your need in the various BUILD_BUG_* macros from the Linux kernel though. They're in include/linux/kernel.h. (Cross-referenced here.)

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