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

fix segmentation fault in trie c++

本文关键字:分段 错误 Trie C++ 修复      更新时间:2023-10-16

我正在使用trie实现来存储和搜索C ++编程语言中的单词。在使用 search(( 函数时,我在搜索特定单词时遇到分句错误。似乎在检查结构是否为空时发生了错误。

以下是错误消息:

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", 
                "answerswer", "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 << "Yesn" : 
                     cout << "Non"; 
    return 0; 
}

@Some程序员Dude和@JohnnyJohansson都指出了根本原因。实时测试显示了代码在何处越界读取数组。实际上,一旦您了解发生了什么,修复就很容易。以下是固定代码,如果您自己无法弄清楚。 它的现场测试在这里 cee.studio

#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", 
                "answerswer", "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 << "Yesn" : 
                     cout << "Non"; 
    return 0; 
}