简体   繁体   English

警告与优化 gcc

[英]Warnings vs optimization gcc

I wonder if the warnings reported by compiler such as variable unused or control reaches end of a non-void function can impact a program (ie crash) when the optimization is enabled (O2 or O3)我想知道编译器报告的警告(例如变量未使用或控制到达非无效 function 的末尾)是否会在启用优化(O2 或 O3)时影响程序(即崩溃)

Could you please give me some examples?你能给我一些例子吗?

Warnings indicate likely mistakes in your code.警告表明您的代码中可能存在错误。 However, whether or not the warning is there or whether or not optimizations are enabled doesn't affect whether the code is correct.但是,是否存在警告或是否启用优化并不影响代码是否正确。

Warnings such as unused variable simply indicate that you probably meant to use the variable somewhere but forgot to do so.诸如unused variable之类的警告只是表明您可能打算在某处使用该变量但忘记了这样做。 Otherwise there wouldn't be a reason for the variable to be there.否则不会有变量存在的理由。

Warnings such as control reaches end of a non-void function are more severe.诸如control reaches end of a non-void function警告更为严重。 For example in this specific case, calling a function with non-void return type causes undefined behavior if its execution reaches the function body's closing } without returning via a return statement prior to that.例如,在这种特定情况下,调用具有非 void 返回类型的 function 会导致未定义的行为,如果其执行到达 function 主体的关闭}而不通过之前的return语句返回。

In this case the warning is informing you that the compiler detected a path that the function could take for some input with this result.在这种情况下,警告会通知您编译器检测到 function 可以使用此结果作为某些输入的路径。 This is very likely unintended, because if you call that function taking that path, then your program would have undefined behavior.这很可能是无意的,因为如果您使用该路径调用 function,那么您的程序将具有未定义的行为。


When the program has undefined behavior you cannot be sure what it will do, absent additional guarantees made by your compiler/platform.当程序具有未定义的行为时,您无法确定它将做什么,因为您的编译器/平台没有额外的保证。 It may behave in one way with optimizations and in a different way without them.它可能在优化时以一种方式运行,而在没有优化时以不同方式运行。 It might also have different behavior with different compiler versions or just between different compilation runs or even program executions.对于不同的编译器版本,或者只是在不同的编译运行甚至程序执行之间,它也可能具有不同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM