xcode 警告“控制可能会到达非空函数的结尾”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19532286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning of "Control may reach end of non-void function"
提问by Cloud_cal
I ran a C++ program in Xcode, and encountered a warning of "Control may reach end of non-void function". Here is the code:
我在 Xcode 中运行了一个 C++ 程序,遇到了“控件可能会到达非空函数的结尾”的警告。这是代码:
Node* search(Node* head, int x)
{
if(!head)
return NULL;
else if(x == head->key)
return head;
else if(x < head->key)
search(head->lchild, x);
else
search(head->rchild, x);
}
I got the same warning when compiling it in Linux, but got the correct result. But in Xcode, the result was wrong. By the way, I got the correct answer and no warning in Visual Studio.
在 Linux 中编译时我收到了同样的警告,但得到了正确的结果。但是在Xcode中,结果是错误的。顺便说一句,我在 Visual Studio 中得到了正确答案并且没有警告。
回答by Fred Larson
I think you mean to return the result of the recursive calls:
我认为您的意思是返回递归调用的结果:
Node* search(Node* head, int x)
{
if(!head)
return NULL;
else if(x == head->key)
return head;
else if(x < head->key)
return search(head->lchild, x);
else
return search(head->rchild, x);
}
回答by Shafik Yaghmour
Your function as it is now allows you to flow off the end of the function with no explicit return which is undefined behavior, the draft standard section 6.6.3
The return statementsays;
您现在的函数允许您在没有显式返回的情况下从函数的末尾流出,这是未定义的行为,标准草案部分6.6.3
返回语句说;
[...]Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.[...]
[...]从函数的末尾流出相当于没有值的返回;这会导致返回值的函数中出现未定义的行为。[...]
the last two elsedo not have a returnstatement:
最后两个else没有return语句:
else if(x < head->key)
search(head->lchild, x); // No return
else
search(head->rchild, x); // No return
// no return
}
so in these cases you will flow off the end without returning a value and thus invoked undefined behavior, it seems like you probably meant to have a returnbut just left them out and really meant this:
因此,在这些情况下,您将在不返回值的情况下流到最后,从而调用未定义的行为,似乎您可能打算返回但只是将它们排除在外,真正的意思是:
else if(x < head->key)
return search(head->lchild, x);
else
return search(head->rchild, x);
回答by syam
else if(x < head->key)
search(head->lchild, x);
else
search(head->rchild, x);
In those branches, you're:
在这些分支中,您是:
- calling
search
recursively - discarding the value it returns
- not returning anything
- 调用
search
递归 - 丢弃它返回的值
- 不返回任何东西
Not returning a value from a function is Undefined Behaviour. The fact that you got correct results is purely accidental and not to be relied upon, the compiler could well choose to format your hard drive or order a pizza instead.
不从函数返回值是未定义的行为。您得到正确结果的事实纯属偶然,不可依赖,编译器很可能会选择格式化您的硬盘驱动器或订购比萨饼。
Fix that by adding return
statements:
通过添加return
语句来解决这个问题:
else if(x < head->key)
return search(head->lchild, x);
else
return search(head->rchild, x);