BST folyamatosan egyre szegmentálás hiba

szavazat
3

EDIT: futás át a gdb ad

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400e4c in Tree::findKey(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, Tree::Node*) ()

Kell egy kis segítség az első BST kódot, kapok egy szegmentációs hiba, azt hiszem, hogy ez egy memória szivárgás? Ha tehát én dont tud hol / hogyan kell rögzíteni itt a kódok, melyek szerintem okozza a problémát. Talán azért, mert én dont volna egy példányt kivitelező felállás még ??

tree.cpp fájl

Tree::Tree()
{
  root = NULL;
}

bool Tree::insert(int k, string s)
{
  return insert(root, k, s);
}
//HELPER Call find data with key function
bool Tree::findKey(string& s, int k)
{
    return findKey(s, k, root);
}
bool Tree::insert(Node*& currentRoot, int k, string s)
{
  if(currentRoot == NULL){
    currentRoot = new Node;
    currentRoot->key = k;
    currentRoot->data = s;
    currentRoot->left = NULL;
    currentRoot->right = NULL;
    return true;
  }
  else if (currentRoot->key == k)
    return false;
  else if (currentRoot->key > k)
    return insert(currentRoot->left, k, s);
  else
    return insert (currentRoot->right,k, s);
}
bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if (currentRoot->key == k){
        s = root->data;
        return true;
    }
    else if (root->key < k)
        return findKey (s, k, root->right);
    else if (root->key > k)
        return findKey (s, k, root->left);
    else
        return false;
}

main.cpp

int main()
{
string sout;
  Tree test;
    test.insert(1, a);
    test.insert(2, b);
    test.insert(3, c);
    test.findKey(sout, 3);
    cout<<sout<<endl;
  return 0;
}
A kérdést 27/04/2011 14:09
a forrás felhasználó
Más nyelveken...                            


2 válasz

szavazat
2

Látok néhány lehetséges segfault whenn nézem a módszert. Gondoljunk csak a szélső esetek.

Mi történik itt?:

Tree test; 
test.findKey(sout, 3);

vagy

Tree test;
test.insert(1, "a");
test.findKey(sout, 3);

Fix ezekben az esetekben, és folytassa.

Válaszolt 27/04/2011 14:19
a forrás felhasználó

szavazat
2

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if (currentRoot->key == k){
        s = root->data;
        return true;
    }
    else if (root->key < k)
        return findKey (s, k, root->right);
    else if (root->key > k)
        return findKey (s, k, root->left);
    else
        return false;
}

Ön mindig a roothelyett currentRoot, így nem igazán ereszkednek le a fáról, és kap egy StackOverflow egy bizonyos ponton. Is, akkor hiányzik az ellenőrzés, ha a currentRootjelentése NULL, mert ha elérheti majd, kapsz egy szép segfault (ez az, amit @tgmath jelentett).

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if(currentRoot == NULL)
        return false;
    // as before ...
}
Válaszolt 27/04/2011 14:24
a forrás felhasználó

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more