2011-05-01 69 views
3

我正在编写一个C++应用程序,以跨歌词歌词的大型数据库进行单词搜索。开始,我以每一个字,并把它变成一个字结构,看起来像这样:在C++中将结构指针设置为空时,存储值消失

struct Word{ 
    char* clean; 
    int size; 
    int position; 
    SongId id; 
    Word* same; 
    Word* diff; 
}; 

我有一个“makeNode”功能,执行以下操作:

  1. 发生在每一个字
  2. 创建一个新的Word结构,并增加了字它
  3. 创建一个Word *称为节点指向新词
  4. 存储指针在哈希表。

在我的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; 
} 
+0

什么是'cleanse'?一个函数?发布其代码。它返回本地char数组吗? – Nawaz 2011-05-01 18:03:07

+0

如果你做某事的时候伤害了,不要这样做。停止使用char *,Word *并使用std :: strings和Word的向量。并重新考虑你的struct的名字 - 为什么叫做Word? – 2011-05-01 18:08:38

回答

2

的问题是可能是在cleanse中,您将返回clean.c_str()

clean停止存在时(即函数退出时),该指针值停止有效。它不再保证指向任何东西,所以你真的很幸运,你会看到字符串“再次”按预期。

我怀疑的情况是,该使用的存储器,可通过数据的字符串cleancleanse占领,又被重新用于结构word,但不会立即覆盖。恰巧,用于保存第一个a的字节现在持有您的结构的same成员的一部分。所以,当你写一个空指针到node->same时,它的效果是写一个0字节到node->clean指向的位置。此后,它似乎指向一个空字符串。

从新
0

好了,我们需要实际看到的代码,其中的一些可以肯定的,但这里的什么错误是告诉你:在某些时候,你指定的东西,覆盖或删除您的清洁。既然你把它声明为一个char *,我猜你会用它作为指向一个字符数组的指针,而且有可能一个数组被混淆为两个不同单词中的两个“干净”指针。

2

您需要将代码缩减为显示问题的最小示例,然后发布。

以下代码无法显示问题。的main和内容Word的定义是从你的代码,在必要时获得,然后我加入的代码复制它来编译:

#include <iostream> 
#include <cstring> 
using namespace std; 

typedef int SongId; 

struct Word{ 
    char* clean; 
    int size; 
    int position; 
    SongId id; 
    Word* same; 
    Word* diff; 
}; 

char *cleanse(const char *w) { 
    return (char *)w; 
} 
const char *word = "again "; 
const int position = 1739; 
const int id = 0; 

int main() { 
    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: again 
node position: 1739 
node size: 6 
0

除了和cout这很可能会成为C.

一些其他读取
What are the differences between struct and class in C++?
char * Vs std::string
Remove spaces from std::string in C++
tolower function for C++ strings
How can I negate a functor in C++ (STL)?

尝试使用以下的替代(未编译样品)

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <functional> 

typedef int SongId; 

class Word{ 
    int position; 
    SongId id; 
    Word* same; 
    Word* diff; 

public: 
    const std::string word; 

    const int size() const { return clean.length() }; 

    Word(const std::string& word_, const int position_ = 1739, const int id_ = 0) 
    : clean(cleanse(word_)) 
    , position(position_) 
    , id(id_) 
    , same(NULL) 
    , diff(NULL) 
    { 
    cout<<"MADE NODE FOR "<< word_ << "\n" 
     <<"node clean: "<< word << "\n" 
     <<"node position: "<< position << "\n"; 
     <<"node size: "<< size() << endl; 
    } 

    static std::string cleanse(const std::string& dirty) 
    { 
    string clean(dirty); 

// Remove anything thats not alpha num 
    clean.erase(remove_if(clean.begin(), clean.end(), std::not1(::isalnum)), clean.end()); 
// make it lower case 
    std::transform(clean.begin(), clean.end(), clean.begin(), ::tolower); // or boost::to_lower(str); 

    return clean; 
    } 
}; 

const char *word = "again "; 

int main() { 
    Word* node = new Word(word); 
}