C++ 搜索功能导致程序崩溃
我一直在使用调试器,但似乎无法准确指出到底出了什么问题。我得出了自己的结论,我一定是在什么地方漏掉了一张空支票。如果有人能提供一些帮助,我们将不胜感激 来自调试器的错误消息 这似乎使程序在这一行崩溃:C++ 搜索功能导致程序崩溃,c++,debugging,pointers,tree,logic,C++,Debugging,Pointers,Tree,Logic,我一直在使用调试器,但似乎无法准确指出到底出了什么问题。我得出了自己的结论,我一定是在什么地方漏掉了一张空支票。如果有人能提供一些帮助,我们将不胜感激 来自调试器的错误消息 这似乎使程序在这一行崩溃: if (node->children_[index] == nullptr) { 搜索功能 Node* search(const string& word, Node* node, int index) const { Node* temp;
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
}
Node*search(常量字符串和单词,Node*Node,int索引)常量{
节点*温度;
//和递归查找相同,只是区别在于是否返回节点天气终端
if(索引getIndex(word[index]);
如果(节点->子节点[index]==nullptr){
返回空ptr;
}
否则{
temp=搜索(单词,节点->子项[index],index++);
}
}
return temp;//这将为您提供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;
}
struct节点{
布尔伊斯特米纳尔;
char ch_;
节点*子节点[26];
节点(字符c='\0'){
isTerminal=假;
ch_=c;
对于(int i=0;i<26;i++){
children_i]=nullptr;
}
}
//给定小写字母字符ch,返回
//关联的索引“a”-->0,“b”-->1…“z”-->25
int getIndex(char-ch){
返回ch-‘a’;
}
};
节点*根;
int suggest(常量字符串和partialWord,字符串建议[])常量{
节点*温度;
temp=搜索(partialWord,root,0);
整数计数=0;
建议(partialWord、temp、建议、计数);
返回计数;
}
可能是一件非常简单的事情。没有挖掘,我不确定->
操作符与=
操作符的排名。我会花一秒钟的时间尝试在“节点->子节点”[index]==nullptr
”部分加上括号,如下所示:
(node->children_[index]) == nullptr
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);
}
}
只是为了确保逻辑按照您的意图运行
Dr t我认为根本原因是您使用索引有两个不同的目的:作为您要查找的单词的索引,以及作为节点子节点的索引。
当您使用递归时,索引
的含义发生了变化,从那以后,一切都在走下坡路
您还将index++
传递给递归,但是index++
的值是它在增量之前的值。
您应该通过索引+1
[另一个程序中的一个问题是,函数参数的求值顺序未指定,您不应该同时修改变量和在同一个参数列表中使用它。(我甚至认为您不应该修改参数列表中的任何内容,但许多人不同意)。
但是您不应该在这里使用相同的变量,所以…]
我个人会对代码进行一些重构,比如:
(node->children_[index]) == nullptr
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
函数返回指向非常量内部节点的指针是有问题的。)按“Break”键并检查调试器中的变量。在递归的某个点上,null几乎肯定会作为节点传递。结构的布局表明子项
位于base+4,实际故障来自尝试在位置0x00000004
读取。调试器在节点->子节点[…]上中断。因此,您可能会评估某个地方缺少空检查。不确定这是否是问题所在,但在递归调用搜索(word,node->children[index],index++)
中,未指定节点->children[index]
和index++
的求值顺序。传递index
,如果这是您的意思,则随后递增。@molbdnilo我几乎相信这就是问题所在。@molbdnilo我使用了您的建议,并在该参数之外递增了索引。当我这样做时,崩溃不会发生,但是索引的返回值是0,当它应该是15时,OP代码中的假设没有问题。->
和[]
都将优先于=
。