cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Seg Fault in BST Insertion
Seg Fault in BST Insertion
Oct 11, 2012 at 3:15am UTC
edison84
(8)
When I call the following binary search tree insert function with the function call
insert(root, "apple"), the compiler seg faults. Is there an explanation?
struct node
{
string data;
node* left;
node* right;
};
void insert(node* &root, string add_me)
{
if (root == NULL)
{
root -> data = add_me;
root -> left = NULL;
root -> right = NULL;
}
else if (add_me <= root -> data)
{
insert(root->left, add_me);
}
else
{
insert(root->right, add_me);
}
}
Oct 11, 2012 at 3:18am UTC
ne555
(10692)
You're purposely dereferencing a NULL pointer...
Oct 11, 2012 at 3:20am UTC
edison84
(8)
I got it. I forgot to allocate dynamic memory.
Topic archived. No new replies allowed.