.
bool Node::lookup(int d){
if (!this)
return false;
return (data == d) || left->lookup(d) || right->lookup(d);
}
EDIT: , - . ? , , :
Node* tree;
if (tree->lookup(i))
printf("found!\n");
Node* tree;
if (tree && tree->lookup(i))
printf("found!\n");
The only way he could create UB is when the compiler considers that thisit cannot be NULL and skip the test. This will not happen because in the wild there is enough code that uses this type of validation. Also, in simple C, this is common practice.
source
share