简体   繁体   English

为什么该程序显示“无法访问的代码”警告? 以及我如何抑制它?

[英]Why is this program showing “unreachable code” warning? And how do I suppress it?

#include<iostream.h>
#include<conio.h>
#include<process.h>

void function(void);

int main(void)
{
 clrscr();

 int ch;

 while(1)
 {
  cin>>ch;

  if(ch==2)
    exit(0);
  else
    function();
 }//while
 return 0;
}//main

void function(void)
{
 cout<<"Hello";

 return;
}

The above code is working fine, but why I'm getting the "unreachable code" warning? 上面的代码工作正常,但是为什么我收到“无法访问的代码”警告? I really don't understand what am I doing wrong. 我真的不明白我在做什么错。 The compiler shows no warning when I comment/remove the return 0; 当我注释/删除return 0;时,编译器未显示警告return 0; statement in main() . main()语句。 Why is it so? 为什么会这样呢? Please tell me what is it that I'm doing wrong and what is the correct way to do it. 请告诉我我做错了什么,做对的正确方法是什么。

The while (1) loop has no option to leave. while (1)循环没有其他选择。

Thereby, the exit(0) is not recognized as the dataflow analysis doesn't consider it as an option to jump to code behind of while (1) (and actually it doesn't). 因此,由于数据流分析不会将exit(0)视为跳至while (1)后面的代码的一种选择(实际上不是),因此无法识别exit(0) )。

Hence, there is no way to get to the return 0; 因此,无法获得return 0; .

If you replace exit(0) by a break than it changes. 如果将break exit(0)替换为break ,则它会改变。 The break will cause to leave the while (1) and the return 0; break将导致while (1)return 0; becomes reachable. 变得可及。

why I'm getting the "unreachable code" warning? 为什么我收到“无法访问的代码”警告? I really don't understand what am I doing wrong. 我真的不明白我在做什么错。

The loop has no return condition: while(1) and the loop body doesn't contain a break (or goto ) that would jump out of the loop othwewise. 循环没有返回条件: while(1)且循环体不包含其他可能会从循环中跳出的break (或goto )。 Nevertheless, you have a return 0; 不过,您的return 0; statement after the loop. 循环后的语句。 Since the execution never jumps out of the loop, it can never reach the return statement. 由于执行永远不会跳出循环,因此永远无法到达return语句。

The compiler warns you that the line has no effect on the behaviour of the program, since the execution can never reach it. 编译器警告您该行对程序的行为没有影响,因为执行永远无法达到目标。 You can get rid of the warning by changing the logic of your program in a way that can potentially jump out of the loop. 您可以通过可能会跳出循环的方式更改程序逻辑来摆脱警告。 I suggest following: 我建议如下:

if(ch==2)
  break;
else
  function();

The compiler shows no warning when I comment/remove the return 0; 当我注释/删除返回值0时,编译器未显示警告; statement in main(). main()中的语句。 Why is it so? 为什么会这样呢?

That statement was the "unreachable code" that the warning referred to. 该声明是警告所指的“无法访问的代码”。 If there is no unreachable code, then there is no need to warn about it. 如果没有无法访问的代码,则无需对其进行警告。

It is safe to remove the line. 删除线路是安全的。 main is special in the regard that it implicitly returns 0 in the absence of return statement (if the execution ever returns from main which your program never does). main是特殊的,因为它在没有return语句的情况下隐式返回0(如果执行曾经从main返回而您的程序从不执行)。

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

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