修复Trie C ++中的分段错误

时间:2019-02-11 08:46:07

标签: c++ trie

我正在使用trie实现以c ++编程语言存储和搜索单词。使用search()函数时,在搜索特定单词时遇到了段错误。似乎在检查struct是否为null时发生了错误。

这是错误消息:

Program received signal SIGSEGV, Segmentation fault.
0x000055555555b2ff in search (this=0x55555577ee70, 
wordlist=0x55555577ef00, word="a1g6os") at test.cc:30
            if (!pCrawl->children[index])

这是源代码:

#include <bits/stdc++.h> 
using namespace std; 
const int ALPHABET_SIZE = 26; 

struct TrieNode { 
struct TrieNode *children[ALPHABET_SIZE]; 

bool isEndOfWord; 
}; 



struct TrieNode *getNode(void) { 
    struct TrieNode *pNode =  new TrieNode; 
     pNode->isEndOfWord = false; 

    for (int i = 0; i < ALPHABET_SIZE; i++) 
        pNode->children[i] = NULL; 

    return pNode; 
} 



void insert(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - 'a'; 
        if (!pCrawl->children[index]) 
            pCrawl->children[index] = getNode(); 

        pCrawl = pCrawl->children[index]; 
   } 

   // mark last node as leaf 
   pCrawl->isEndOfWord = true; 
} 

// Returns true if key presents in trie, else 
// false 
bool search(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - 'a'; 
        if (!pCrawl->children[index]) 
             return false; 

        pCrawl = pCrawl->children[index]; 
    } 

    return (pCrawl != NULL && pCrawl->isEndOfWord); 
} 

int main() { 
    string keys[] = {"the", "a", "there", 
                "answer", "any", "by", 
                 "bye", "their" }; 
    int n = sizeof(keys)/sizeof(keys[0]); 

    struct TrieNode *root = getNode(); 

    for (int i = 0; i < n; i++) 
        insert(root, keys[i]); 

    // Search for different keys 
    search(root, "a1g6os")? cout << "Yes\n" : 
                     cout << "No\n"; 
    return 0; 
}

1 个答案:

答案 0 :(得分:2)

@Some程序员dude和@JohnnyJohansson都指出了根本原因。实时测试显示了代码在哪里越界读取数组。实际上,一旦您了解会发生什么,修复便很容易。如果您自己无法弄清楚以下代码,则为固定代码。 segfault.stensal.com

在这里进行了实时测试
#include<iostream>
using namespace std; 
const int ALPHABET_SIZE = 75; // increase the range

struct TrieNode { 
struct TrieNode *children[ALPHABET_SIZE]; 

bool isEndOfWord; 
}; 



struct TrieNode *getNode(void) { 
    struct TrieNode *pNode =  new TrieNode; 
     pNode->isEndOfWord = false; 

    for (int i = 0; i < ALPHABET_SIZE; i++) 
        pNode->children[i] = NULL; 

    return pNode; 
} 



void insert(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - '0';  // lower the low bound
        if (!pCrawl->children[index]) 
            pCrawl->children[index] = getNode(); 

        pCrawl = pCrawl->children[index]; 
   } 

   // mark last node as leaf 
   pCrawl->isEndOfWord = true; 
} 

// Returns true if key presents in trie, else 
// false 
bool search(struct TrieNode *root, string key) { 
    struct TrieNode *pCrawl = root; 

    for (int i = 0; i < key.length(); i++) { 
        int index = key[i] - '0';  // lower the low bound
        if (!pCrawl->children[index]) 
             return false; 

        pCrawl = pCrawl->children[index]; 
    } 

    return (pCrawl != NULL && pCrawl->isEndOfWord); 
} 

int main() { 
    string keys[] = {"the", "a", "there", 
                "answer", "any", "by", 
                 "bye", "their" }; 
    int n = sizeof(keys)/sizeof(keys[0]); 

    struct TrieNode *root = getNode(); 

    for (int i = 0; i < n; i++) 
        insert(root, keys[i]); 

    // Search for different keys 
    search(root, "a1g6os")? cout << "Yes\n" : 
                     cout << "No\n"; 
    return 0; 
}