【问题标题】:search function causes program to crash搜索功能导致程序崩溃
【发布时间】:2017-04-20 19:22:50
【问题描述】:

我一直在调试调试器,但似乎无法准确指出出了什么问题。我得出了自己的结论,我一定是在某处或某处遗漏了 nullptr 检查。如果有人可以提供一些帮助,将不胜感激。

来自调试器的错误消息

error msg

这看起来会让程序在这一行崩溃:

if (node->children_[index] == nullptr) {

搜索功能

    Node* search(const string& word, Node* node, int index) const {
            Node* temp;
            //same as recurssive lookup just difference is returns node weather terminal or not
            if (index < word.length()) {


                            index = node->getIndex(word[index]);


                            if (node->children_[index] == nullptr) {
                                    return nullptr;
                            }
                            else {
                                    temp = search(word, node->children_[index], index++);
                            }
                    }

                    return temp; // this would give you ending node of partialWord


            }

节点结构供参考

    struct Node {
            bool isTerminal_;
            char ch_;
            Node* children_[26];
            Node(char c = '\0') {
                    isTerminal_ = false;
                    ch_ = c;
                    for (int i = 0; i < 26; i++) {
                            children_[i] = nullptr;
                    }
            }
            //given lower case alphabetic charachters ch, returns 
            //the associated index 'a' --> 0, 'b' --> 1...'z' --> 25
            int getIndex(char ch) {
                    return ch - 'a';

            }
    };
    Node* root_;


int suggest(const string& partialWord, string suggestions[]) const {

    Node* temp;

    temp = search(partialWord, root_, 0);

    int count = 0;
    suggest(partialWord, temp, suggestions, count);

    return count;
}

【问题讨论】:

  • 按“Break”并检查调试器中的变量。
  • 在您的递归中,null 几乎肯定会以node 的形式传递。您的结构布局表明children 位于基数+4,而实际错误来自尝试读取位置0x00000004。调试器在node-&gt;children[...] 上中断。因此,您可以评估在某处遗漏空检查。
  • 不确定这是否是问题所在,但在递归调用 search(word, node-&gt;children_[index], index++) 中,node-&gt;children_[index] 和 index++ 的评估顺序未指定。传递index 并在之后增加,如果这就是你的意思。
  • @molbdnilo 我几乎会认为这是问题所在。
  • @ molbdnilo 我使用了您的建议并在该参数之外增加了索引。当我这样做时不会发生崩溃,但索引的返回值为 0,而它应该是 15

标签: c++ debugging pointers tree logic


【解决方案1】:

可能是一件非常简单的事情。没有挖掘,我不确定-&gt; 运算符与== 运算符的排名。我会花一点时间尝试在“node-&gt;children_[index] == nullptr”部分加上括号,如下所示:

(node->children_[index]) == nullptr

只是为了确保逻辑按照您的意图运行。

博士

【讨论】:

  • OP 代码中的operator precedence 假设没有任何问题。-&gt; 和[] 都将优先于==。
【解决方案2】:

我认为根本原因是您将index 用于两个不同的目的:作为您要查找的单词的索引,以及作为节点子节点的索引。
当您进行递归时,index 的含义发生了变化,并且从那里开始走下坡路。

您还将index++ 传递给递归,但index++ 的值是它在递增之前的值。
你应该通过index + 1。

[在不同的程序中的一个问题是函数参数的求值顺序是未指定的,你不应该同时修改一个变量并在同一个参数列表中使用它。 (我什至会说你永远不应该修改参数列表中的任何东西,但很多人不同意。)
但是你根本不应该在这里使用相同的变量,所以...]

我个人会稍微重构一下代码,如下所示:

Node* search(const string& word, Node* node, int index) const {
    // Return immediately on failure.
    if (index >= word.length())
    {
        return nullptr;
    }

    int child_index = node->getIndex(word[index]);

    // The two interesting cases: we either have this child or we don't.
    if (node->children_[child_index] == nullptr) {
        return nullptr;
    }
    else {
        return search(word, node->children_[child_index], index + 1);
    }
}

(旁注:从 const 函数返回指向非 const 内部 Node 的指针是有问题的。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多