简体   繁体   English

如果没有从具有返回类型 integer 或指针的 function 中的 c++ 显式返回任何值,则从 function 隐式返回什么值

[英]What value is returned from a function implicitly if no value is returned explicitly from a function in c++ that has a return type integer or pointer

This is a code to find least common ancestor of n1 and n2 in a binary tree这是在二叉树中查找n1n2的最小公共祖先的代码

Node * lca (Node* root, int n1, int n2)
{
  if (root == NULL)
    return NULL;

  if (root->data == n1 or root->data == n2)
    return root;
  
  Node *l = lca(root->left, n1, n2);
  Node *r = lca(root->right, n1, n2);
  
  if (l and r)
    return root;

  //return (l != NULL) ? l : r;
}

I took a random binary tree as shown below我拿了一个随机的二叉树,如下所示

    1
   / \
  2   3
 / \ / \
4  5 6  7

When the function call goes to node with value 5 there is no pointer to node that is returned to the caller function. So what value will be assigned to r in that case?当 function 调用转到值为 5 的节点时,没有指向返回给调用者 function 的节点的指针。那么在这种情况下, r分配什么值?

I tried assuming that by default it returns a NULL pointer, so if (r == NULL) , I tried to print a number, but I got no value in output.我尝试假设默认情况下它返回一个 NULL 指针,所以if (r == NULL) ,我尝试打印一个数字,但我在 output 中没有得到任何值。

I tried using the keyword undefined like in javascript but to no avail.我尝试像 javascript 那样使用关键字undefined但无济于事。 It is showing error.它显示错误。 what does it mean to not return any value in a function that has a return type?在具有返回类型的 function 中不返回任何值是什么意思?

If you have function that is declared with a return type other than void and in a call to the function the closing } is reached without an intervening return statement, then your program has undefined behavior .如果您的 function 声明了除void以外的返回类型,并且在调用 function 时在没有中间return语句的情况下达到结束} ,则您的程序具有未定义的行为

Undefined behavior means that you lose any guarantees on the behavior of the program.未定义的行为意味着您失去了对程序行为的任何保证。 Any outcome is possible.任何结果都是可能的。 It could behave as if you had written return NULL;它的行为就像你写了return NULL; , it could behave as if returning a random value, it could behave as if the call never happened and it doesn't have to be consistent in any way. ,它可以表现得好像返回一个随机值,它可以表现得好像调用从未发生过并且它不必以任何方式保持一致。 This is true not only for the offending function call, but the whole program (with given input), also the parts leading up to the function call.这不仅适用于有问题的 function 调用,而且适用于整个程序(具有给定输入),以及导致 function 调用的部分。 Such a program is simply invalid and broken.这样的程序根本无效且损坏。 It is pointless to think about its behavior.考虑它的行为是没有意义的。

So don't do that.所以不要那样做。 Compilers will warn you about the possibility of running off the end of a non- void function if you enable warnings.如果启用警告,编译器将警告您可能会超出非void function 的末尾。 If yours didn't, then check how to enable a higher warning level for your compiler.如果你的没有,那么检查如何为你的编译器启用更高的警告级别。

I tried using the keyword undefined like in javascript but to no avail.我尝试像 javascript 那样使用关键字 undefined 但无济于事。

There is no equivalent to that in C++.没有与 C++ 中的等效项。

Also, if you are coming from javascript, then you need to be especially careful, since javascript (as far as I know) has no equivalent of C's and C++'s undefined behavior .此外,如果您来自 javascript,那么您需要特别小心,因为 javascript(据我所知)没有等效于 C 和 C++ 的未定义行为 Understanding the significance of this concept and remembering which situations cause it, is something that new users of the language coming from other languages often seem to have problems with.理解这个概念的重要性并记住是什么情况导致的,是来自其他语言的新用户似乎经常遇到的问题。 In C and C++ you are not guaranteed to be notified with a warning or error at either compile- or runtime if your program is broken.在 C 和 C++ 中,如果您的程序被破坏,您不能保证在编译或运行时收到警告或错误通知。 It will simply have arbitrary behavior that may or may not seem to work as expected under undefined behavior .它只会有任意行为,在未定义的行为下可能会或可能不会按预期工作。

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

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