Bus Error (after updating my macbook pro to sonoma 14.0)

166 Views Asked by At

I recently updated my macbook to Sonoma 14.0 and am unable to run this program for a BST. It used to run just fine before I updated my laptop and now I'm getting a bus error. I'm fairly new at coding and don't know what to do to fix this. I would appreciate some help.

#include <iostream>

using namespace std; 

template<typename Char> class BST {
public:  
    struct Node {
        Char data; 
        Node* left;
        Node* right;
    };
    Node* rootNode;

    BST() {
        rootNode->data = '\0';
        rootNode->left = nullptr;
        rootNode->right = nullptr;
    }

    Node* createNode(char ch) {
        Node* nn = new Node;
        nn->data = ch;
        nn->left = nullptr;
        nn->right = nullptr;
        return nn;
    }

    void insert(char myChar) {
        insertHelper(rootNode, myChar);
    }

    void inOrderTraversal() {
        inOrderTraversalHelper(rootNode);
    }

    void preOrderTraversal() {
        preOrderTraversalHelper(rootNode);
    }

    void postOrderTraversal() {
        postOrderTraversalHelper(rootNode);
    }    

private:
    char insertHelper(Node* &root, char ch) {
        if (root->data == '\0') {
            root->data = ch;
        } else if ( ch < root->data) {
            if (root -> left == nullptr) {
                root -> left = createNode(ch);
            } else {
                insertHelper(root->left, ch);
            } 
        } else {
            if ( ch > root->data) {
                if (root -> right == nullptr) {
                root -> right = createNode(ch);
            } else {
                insertHelper(root->right, ch);
            }
            }
        } return ch;
    }

    void inOrderTraversalHelper(Node* node) {
        if (node != nullptr) {
            inOrderTraversalHelper(node->left); 
            cout << node->data << " ";           
            inOrderTraversalHelper(node->right); 
        }
    }

    void preOrderTraversalHelper(Node* node) {
        if (node != nullptr) {
            cout << node->data << " ";           
            preOrderTraversalHelper(node->left);  
            preOrderTraversalHelper(node->right); 
        }
    }

    void postOrderTraversalHelper(Node* node) {
        if (node != nullptr) {
            postOrderTraversalHelper(node->left); 
            postOrderTraversalHelper(node->right);
            cout << node->data << " ";          
        }
    }
};

int main () {
    BST<char> cisp_bst; 
    cisp_bst.insert('k');
    cisp_bst.insert('e');
    cisp_bst.insert('v');
    cisp_bst.insert('b');
    cisp_bst.insert('g');
    cisp_bst.insert('p');
    cisp_bst.insert('y');

    cout << "\n" << "In order traversal: ";
    cisp_bst.inOrderTraversal();
    cout << "\n\n";
    cout << "Post-order traversal: ";
    cisp_bst.postOrderTraversal();
    cout << "\n\n";
    cout << "Pre-order traversal: ";
    cisp_bst.preOrderTraversal();
    cout << "\n\n";

    return 0;
};

It ran just fine before but now it gives me a bus error. The debugger says it crashes in the first line of the constructor. (assigning data to '\0')

1

There are 1 best solutions below

0
On

I try to debug with lldb on my macbook(also Sonoma 14.0), and get the error message:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x100002bbc)
    frame #0: 0x0000000100002dfc a.out`BST<char>::BST(this=0x000000016fdfefc0) at test.cpp:15:20
   12     Node *rootNode;
   13  
   14     BST() {
-> 15       rootNode->data = '\0';
   16       rootNode->left = nullptr;
   17       rootNode->right = nullptr;
   18     }
Target 1: (a.out) stopped.

EXC_BAD_ACCESS means that your program is trying to access memory that it doesn't have permission to access. And for your code, it happens in the constructor of your BST class.

So the solution is:

  1. Change your BST() from
BST() {
    rootNode->data = '\0';
    rootNode->left = nullptr;
    rootNode->right = nullptr;
}

To:

BST() {
    rootNode = nullptr;
}
  1. And change the first if-statement of the insertHelper() from:
if (root->data == '\0') {
    root->data = ch;
}

To:

if (root == nullptr) {
   root = createNode(ch);
} 

This method works on my macbook, and I got this result:


In order traversal: b e g k p v y 

Post-order traversal: b g e p y v k 

Pre-order traversal: k e b g v p y 

Hope this also works for you.