I'm attempting to build a suffix tree in C++ as part of an assignment on gene sequencing
void Tree::insert(string ins)
{
Node* iterator = chooseBranch(root, ins.at(0));
string temp;
for(int i=0; i<100; i++)
{
if(iterator->data=="")
.
.
.
chooseBranch() is a function to pick which of 4 children to go to, and I'm attempting to check if this Node already exists. My Node class is:
struct Node{
Node();
string data;
Node* A;
Node* G;
Node* C;
Node* T;
};
This if statement is giving me a segfault, which I used gdb to backtrack to:
#0 0x0000003ce249bbd6 in std::string::compare () from /usr/lib64/libstdc++.so.6
#1 0x000000000040185b in std::operator==<char, std::char_traits<char>, std::allocator<char> > ()
#2 0x0000000000401305 in Tree::insert ()
#3 0x00000000004016d4 in Tree::Tree ()
#4 0x00000000004010a2 in main ()
What is wrong with this form of NULL checking/how else could I check if the Node has no data?
It doesn't look like you are checking the pointer iterator for NULL at all, you are simply dereferencing it (which will cause drama if it is NULL).
Here's a sample to try, with the check for NULL hoisted out of the for loop:
void Tree::insert(string ins)
{
Node* iterator = chooseBranch(root, ins.at(0));
if (iterator)
{
string temp;
for(int i=0; idata=="")
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With