C+中缺少return语句+;还能用吗 我从博客中发现了一些C++代码。我在IDE中测试了代码。即使在递归调用(搜索(head->lchild,x)和搜索(head->rchild,x))中没有return语句,代码中的函数也可以正常工作;有人能解释为什么吗 Node* search(Node* head, int x){ if(head == NULL) return NULL; else if(x == head->key) return head; else if(x <= head->key) search(head->lchild, x); else search(head->rchild, x); } Node*搜索(Node*head,int x){ if(head==NULL)返回NULL; 否则如果(x==头部->键)返回头部; 否则,如果(x键)搜索(head->lchild,x); else搜索(head->rchild,x); }

C+中缺少return语句+;还能用吗 我从博客中发现了一些C++代码。我在IDE中测试了代码。即使在递归调用(搜索(head->lchild,x)和搜索(head->rchild,x))中没有return语句,代码中的函数也可以正常工作;有人能解释为什么吗 Node* search(Node* head, int x){ if(head == NULL) return NULL; else if(x == head->key) return head; else if(x <= head->key) search(head->lchild, x); else search(head->rchild, x); } Node*搜索(Node*head,int x){ if(head==NULL)返回NULL; 否则如果(x==头部->键)返回头部; 否则,如果(x键)搜索(head->lchild,x); else搜索(head->rchild,x); },c++,recursion,return-value,C++,Recursion,Return Value,定义一个没有返回语句的非void函数有一个未定义的Bihavour,与没有表达式的返回语句有相同的想法 C++11在§6.6.3中说: 6.6.3 The return statement 2 A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with

定义一个没有返回语句的非void函数有一个未定义的Bihavour,与没有表达式的返回语句有相同的想法

C++11在
§6.6.3
中说:

6.6.3 The return statement

2 A return statement with neither an expression nor a braced-init-list can be
used only in functions that do not return a value, that is, a function with
the return type void, a constructor (12.1), or a destructor (12.4).

...

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.
您必须在编译器中打开警告:

g++ -Wall
您将得到以下错误:

warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

定义一个没有返回语句的非void函数有一个未定义的Bihavour,并且与没有表达式的返回语句有相同的想法

C++11在
§6.6.3
中说:

6.6.3 The return statement

2 A return statement with neither an expression nor a braced-init-list can be
used only in functions that do not return a value, that is, a function with
the return type void, a constructor (12.1), or a destructor (12.4).

...

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.
您必须在编译器中打开警告:

g++ -Wall
您将得到以下错误:

warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

您的代码具有未定义的行为,因此任何事情都可能发生,包括执行您想要的操作。在叮当声下,你不会得到你期望的结果

我做了最小的修改,以使您的代码能够编译并使用未定义的行为执行路径:

struct Node { int key; Node *lchild; Node *rchild; };
Node *search(Node *head, int x){
  if(head == nullptr) return nullptr;
  else if(x == head->key) return head;
  else if(x <= head->key) search(head->lchild, x);
  else search(head->rchild, x);
}
Node a { 0, nullptr, nullptr };
Node b { 1, &a, nullptr };
int main() { search(&b, 0); }
使用Clang的
-fsanize=undefined
,我得到了以下结果:

$ clang++ -std=c++11 -w -fsanitize=undefined wszdwp.cpp && ./a.out
wszdwp.cpp:2:7: runtime error: execution reached the end of a value-returning function without returning a value

代码可能对您有效,因为您的编译器在函数体的末尾“有益地”输入了
ret
指令(没有填写返回值reigster,因此返回值很可能是从您调用的前一个函数继承的)。

您的代码有未定义的行为,因此可能发生任何事情,包括做你想做的事。在叮当声下,你不会得到你期望的结果

我做了最小的修改,以使您的代码能够编译并使用未定义的行为执行路径:

struct Node { int key; Node *lchild; Node *rchild; };
Node *search(Node *head, int x){
  if(head == nullptr) return nullptr;
  else if(x == head->key) return head;
  else if(x <= head->key) search(head->lchild, x);
  else search(head->rchild, x);
}
Node a { 0, nullptr, nullptr };
Node b { 1, &a, nullptr };
int main() { search(&b, 0); }
使用Clang的
-fsanize=undefined
,我得到了以下结果:

$ clang++ -std=c++11 -w -fsanitize=undefined wszdwp.cpp && ./a.out
wszdwp.cpp:2:7: runtime error: execution reached the end of a value-returning function without returning a value

代码可能对您有用,因为您的编译器在函数体的末尾“有用”地输入了
ret
指令(没有填写返回值reigster,因此返回值可能是从您调用的前一个函数继承的)。

这是未定义的行为。看起来可以工作是一个有效的选项。打开警告?@ShafikYaghmour,不好意思,这个问题的标题对于查找被复制者来说不太有用。您正在使用哪个IDE?@chris您是正确的,它没有那么有用。这是未定义的行为。看起来可以工作是一个有效的选项。打开警告?@ShafikYaghmour,不好意思,这个问题的标题对于查找副本来说不是太有用。您使用的是哪个IDE?@chris您是正确的,它没有那么有用。