I’m getting a segmentation fault when I try to create a new Node object with the pointer variable *temp in line 15 below. I’m still pretty new to c++ and how double pointers work, especially when used in combination with &. Thanks for any help.
void bst::insert(int n) {
Node **temp;
Node *r, *parent;
// Tree is empty
if (root == NULL) {
root = new Node;
root->parent = NULL;
root->value = n;
root->left = NULL;
root->right = NULL;
root->isBlack = true;
} else {
r = root;
// Create a node with the given value, n
(*temp) = new Node;
(*temp)->value = n;
(*temp)->left = NULL;
(*temp)->right = NULL;
(*temp)->isBlack = false;
The variable
tempis not initialized. Therefore, trying to dereferencetempwill fail, as there is no value to dereference. If you really need a pointer to a pointer, you could just declare the single pointer and use the&operator to get the double pointer.