简体   繁体   English

为什么即使保证返回,我也会收到警告“控制到达非空函数的结尾”

[英]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.如果您使用常量,警告就会消失,因为编译器将使用常量值组装代码。

You can check this behavior here .您可以在此处检查此行为。

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.

相关问题 警告:控制到达非无效功能的结尾 - warning: control reaches end of non-void function 如何解决“控件到达非void函数结束”警告? - How to solve the “control reaches end of non-void function” warning? 警告:控制到达非空函数(gtk)的结尾 - warning: control reaches end of non-void function (gtk) “警告:控制到达非空函数的末尾”,但实际上该函数被声明为int并返回一个int - “warning: control reaches end of non-void function” but actually the function is declared as int and return an int 为什么主函数会出现“控制到达非空函数结束”的警告? - Why a warning of “control reaches end of non-void function” for the main function? 当调用带有警告“控制到达非空函数结束”的函数时(实际上)会发生什么? - What (actually) happens, when a function with the warning "control reaches end of non-void function" is called? 为什么我收到此编译 r 错误:控制到达非无效 function [-Werror=return-type] 的结尾 - Why I am getting this Compilation r error: control reaches end of non-void function [-Werror=return-type] 警告:控制到达非空函数的结尾 [-Wreturn-type] - warning: control reaches end of non-void function [-Wreturn-type] Carboard.c:12:1:警告:控制权到达非空函数的结尾[-Wreturn-type] - Carboard.c:12:1: warning: control reaches end of non-void function [-Wreturn-type] 警告:控制到达非空函数的结尾 [-Wreturn-type] - warning: control reaches end of non-void function [-Wreturn-type]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM