我正在编写一个C++应用程序,以跨歌词歌词的大型数据库进行单词搜索。开始,我以每一个字,并把它变成一个字结构,看起来像这样:在C++中将结构指针设置为空时,存储值消失
struct Word{
char* clean;
int size;
int position;
SongId id;
Word* same;
Word* diff;
};
我有一个“makeNode”功能,执行以下操作:
- 发生在每一个字
- 创建一个新的Word结构,并增加了字它
- 创建一个Word *称为节点指向新词
- 存储指针在哈希表。
在我的makeNode函数中,我将node-> clean设置为我的“干净”字。我可以通过cout'ing node-> clean打印这个单词。但是,当我将node->设置为NULL时,我会丢失node-> clean。我不会失去节点 - >位置或节点 - >大小。如果我删除了将node-> same分配给NULL的行,我不会丢失node-> clean。
char* clean = cleanse(word);
Word* node = new Word;
node->size = strlen(word);
node->clean = clean;
cout<<"MADE NODE FOR "<<node->clean<<endl;
node->position = position;
cout<<"4 node clean: "<<node->clean<<endl;
node->id = id;
cout<<"5 node clean: "<<node->clean<<endl;
node->same = NULL;
cout<<"6 node clean: "<<node->clean<<endl;
cout<<"node position: "<<node->position<<endl;
cout<<"node size: "<<node->size<<endl;
node->diff = NULL;
产生以下的输出:
MADE NODE FOR again
4 node clean: again
5 node clean: again
6 node clean:
node position: 1739
node size: 6
0 node clean:
1 node clean:
3 node clean:
谁能帮我把过去的这个错误?如果您需要更多信息,请告诉我。提前致谢!
编辑:这里是清理功能。
char* SongSearch::cleanse(char* dirty)
{
string clean;
int iter = 0;
while (!isalnum(dirty[iter]))
{
iter++;
}
while(dirty[iter]!='\0')
{
clean += dirty[iter];
iter++;
}
int backiter = clean.length() - 1;
while(!isalnum(clean[backiter]))
{
clean.erase(backiter, 1);
backiter--;
}
char c;
for (int i = 0; i<clean.length(); i++)
{
c = tolower(clean[i]);
clean[i] = c;
}
char* toReturn = (char*)(clean.c_str());
return toReturn;
}
什么是'cleanse'?一个函数?发布其代码。它返回本地char数组吗? – Nawaz 2011-05-01 18:03:07
如果你做某事的时候伤害了,不要这样做。停止使用char *,Word *并使用std :: strings和Word的向量。并重新考虑你的struct的名字 - 为什么叫做Word? – 2011-05-01 18:08:38