mardi 17 août 2021

Segment when comparing pointer to nullptr

I have the following code which generates a segmentation fault.

int Node::bfactor() const {
    std::cout<<"fun";
    if( right == nullptr && left == nullptr ) {
        std::cout<<"2";
        return 0;
    } else if ( right == nullptr && left != nullptr) {
        std::cout<<"3";
        return left->height + 1;
    } else if ( left == nullptr && right != nullptr ) {
        std::cout<<"4";
        return right->height + 1;
    } else if ( left != nullptr && right != nullptr ) {
        std::cout<<"5";
        return right->height - left->height;
    }
    std::cout<<"6";
    return 0;
}

And the output is ...fun and then a segmentation fault occurs.

Apparently, the program did not even go into any of the if-statements.

What could possibly be generating a segmentation fault here?

Aucun commentaire:

Enregistrer un commentaire