Your code has undefined behavior, so anything could happen, including doing what you intended. Under Clang, you don't get the result you expected.
I made minimal modifications to get your code to compile and exercise the path with undefined behavior:
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 warns on your code by default, and causes your code to crash when it falls off the end of the function at -O0
:
$ clang++ -std=c++11 wszdwp.cpp
wszdwp.cpp:7:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
1 warning generated.
$ ./a.out
zsh: illegal hardware instruction (core dumped) ./a.out
With Clang's -fsanitize=undefined
, I get this:
$ 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
The code was probably working for you because your compiler "helpfully" put in a ret
instruction at the end of the body of the function (without filling in the return value reigster, so the return value is likely to be inherited from the previous function you called).