禁用 GCC“可以在未初始化的情况下使用”对特定变量
我在堆栈变量上收到此警告:
warning: object.member may be used uninitialized in this function
在这种情况下,我不希望强制初始化只是为了消除警告,因为它会消耗 CPU 周期。该变量是一个 POD 结构,因此其上的 memset
不是零成本。我可以验证该变量永远不会在未初始化的情况下使用,所以我只想抑制它的警告。
一般来说,我确实想要警告,但不是在这个特定场景中的这个特定变量上。我怎样才能抑制警告?
看起来编译指示诊断是正确的方法,但它们需要最新版本的 GCC (4.6)
在该版本之前没有可接受的解决方案。
I'm getting this warning on a stack variable:
warning: object.member may be used uninitialized in this function
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
接受的答案有两个大问题,需要的不仅仅是评论。
首先,它停用整个文件的警告。如果该编译指示位于标头中,则可能有更多内容。警告很有用,如果确实是误报,则应该对一堆尽可能小的代码禁用警告。
然后OP中的警告是“也许未初始化”,它由-Wmaybe-uninitialized(而不是-Wuninitialized)停用。
The accepted answer has two big problems that requires more than a comment.
First, it deactivates the warning for the whole file. If that pragma resides in a header, probably for more. Warnings are useful and if it is indeed a false positive, one should disable the warning for a bunch of code as small as possible.
Then the warning in the OP is "maybe uninitialized" which is deactivated by -Wmaybe-uninitialized, as opposed to -Wuninitialized.
尝试这样做:
这个编译指示具有三种有趣且有用的风格:
警告
、错误
、忽略
。有关其用法,请参阅 6.56.10 诊断编译指示。链接说,Try doing this:
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 区分未初始化和自初始化,例如编译:
使用
gcc -Wall -Wextra
不会给出任何警告,除非您也显式添加-Winit-self
,但它完全得到了通过我的快速测试进行了优化。GCC differentiates between uninitalised and self initalized, e.g. compiling:
With
gcc -Wall -Wextra
gives no warnings, unless you explicitly added-Winit-self
as well, yet it gets completely optimized out by my quick testing.@Nawaz 已经按照具体要求回答了这个问题,但是您是否考虑过您需要这个的事实可能表明您声明您的
struct
太早/嵌套范围比适当的少?如果您可以在可以实际初始化它的位置声明您的 struct,而不是提前声明它并将其填充到各个位置,那么通常会更可取。另外,即使您现在可以验证它从未在未初始化的情况下使用,但如果其他人将来添加新的代码路径并且它未正确初始化怎么办?如果您禁用该警告,那么它会默默地编译并可能以意外的方式中断。除非您能够证明初始化占用了程序的 CPU 的可测量量,否则最好提前进行初始化。
@Nawaz has answered the question as specifically asked, but have you considered that the fact that you need this may indicate you're declaring your
struct
too early/at a less nested scope than appropriate? It would generally be much preferred if you could declare yourstruct
at a point where you can actually initialize it rather than declaring it earlier and filling it in various locations.Also, even though you can verify that it's never used uninitialized right now, what if someone else adds a new code path in the future and it's not initialized properly? If you disable the warning then it'll silently compile and probably break in an unexpected way. Unless you can prove that the initialization is taking a measurable amount of your program's CPU it's probably better to just do the initialization up front.