禁用GCC“可以未初始化使用”。在特定变量上 [英] Disable GCC "may be used uninitialized" on a particular variable

查看:103
本文介绍了禁用GCC“可以未初始化使用”。在特定变量上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在堆栈变量上收到此警告:

I'm getting this warning on a stack variable:

warning: object.member may be used uninitialized in this function

在这种情况下,我不希望强制初始化只是为了消除警告,因为它消耗了CPU周期。变量是一个POD结构,因此 memset 的成本不是零。我可以验证从未使用过该变量,所以我只想禁止它发出警告。

In this case I do not wish to force initialization to just to get rid of the warning as it consumes CPU cycles. The variable is a POD structure so a memset on it is not zero cost. I can verify that the variable is never used uninitialized, so I'd just like to suppress the warning for it.

通常,我确实希望得到警告,只是不要在这种特定情况下,该特定变量。如何抑制警告?

In general I do want the warning, just not on this particular variable in this particular scenario. How can I suppress the warning?

看起来像是实用诊断方法是正确的方法,但它们需要很长时间才能使用GCC版本(4.6)

Looks like the pragma diagnostics are the correct way to go but they require quite a recent version of GCC (4.6)

在该版本之前没有可接受的解决方案。

No acceptable solution prior that version is known.

推荐答案

尝试执行以下操作:

 #pragma GCC diagnostic ignored "-Wuninitialized"
        foo(b);         /* no diagnostic for this one */

此实用程序有三种有趣且有用的口味:警告错误被忽略。有关用法,请参见 6.56.10诊断实用程序。链接显示,

This pragma comes in three interesting and helpful flavors : warning, error, ignored. See 6.56.10 Diagnostic Pragmas for their usages. The link says,


GCC允许用户有选择地
启用或禁用某些类型的
诊断,并更改
诊断的类型。
例如,
项目的策略可能要求
所有源均使用-Werror进行编译,但某些文件
可能具有异常
允许特定类型的警告。
或者,一个项目可能有选择地启用
诊断并将其视为错误
,具体取决于定义了哪些预处理程序宏

GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project's policy might require that all sources compile with -Werror but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

这篇关于禁用GCC“可以未初始化使用”。在特定变量上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆