[英]Why do I get the warning 'control reaches end of non-void function' even when the return is guaranteed
I understand that, given the code never enters the loop, would give me the error.我明白,鉴于代码永远不会进入循环,会给我错误。 However, given that r
is equal to 1
, it should return it, ie , it would always enter it.然而,假设r
等于1
,它应该返回它,即它总是输入它。
int arroz() {
int r = 1;
while (r) {
return r;
}
}
So, why does this code give me the warning control reaches end of non-void function?那么,为什么这段代码会给我警告控制到达非空函数的结尾?
The compiler correctly assumes that r
is a variable value, it does not process it as a constant, that being the case it cannot be sure what the value is going to be and therefore if the body of the cycle will or will not be executed.编译器正确地假定r
是一个变量值,它不会将其作为常量处理,在这种情况下,它无法确定该值将是什么,因此无法确定循环体是否会被执行。
If you use a constant the warning goes away because the compiler will assemble the code using a constant value.如果您使用常量,警告就会消失,因为编译器将使用常量值组装代码。
IMO it is a bug in the compiler. IMO 这是编译器中的一个错误。 The analyzer should see that there is no execution path avoiding the return
statement.分析器应该看到没有避免return
语句的执行路径。 The variable is not volatile
.该变量不是volatile
。
The code is compiled to:代码编译为:
arroz: # @arroz
mov eax, 1
ret
This shows that compiler know that it will always return 1这表明编译器知道它总是会返回 1
You should report the bug.你应该报告错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.