在C+;中将结构指针设置为null时,存储值消失+; 我写了一个C++应用程序,在一个大型的歌词数据库中进行单词搜索。首先,我将每个单词放入一个单词结构中,如下所示: struct Word{ char* clean; int size; int position; SongId id; Word* same; Word* diff; };

在C+;中将结构指针设置为null时,存储值消失+; 我写了一个C++应用程序,在一个大型的歌词数据库中进行单词搜索。首先,我将每个单词放入一个单词结构中,如下所示: struct Word{ char* clean; int size; int position; SongId id; Word* same; Word* diff; };,c++,pointers,hashtable,cstring,C++,Pointers,Hashtable,Cstring,我有一个“makeNode”函数,它执行以下操作: 每一个字都能理解 创建一个新的单词结构并将单词添加到其中 创建一个名为node的单词*,它指向新词 将指针存储在哈希表中 在makeNode函数中,我将node->clean设置为我的“clean”字。我可以通过cout'ting node->clean打印单词。但当我将node->same设置为NULL时,我将丢失node->clean。我不会丢失节点->位置或节点->大小。如果删除将node->same赋值为NULL的行,则不会丢失node

我有一个“makeNode”函数,它执行以下操作:

  • 每一个字都能理解
  • 创建一个新的单词结构并将单词添加到其中
  • 创建一个名为node的单词*,它指向新词
  • 将指针存储在哈希表中
  • 在makeNode函数中,我将node->clean设置为我的“clean”字。我可以通过cout'ting node->clean打印单词。但当我将node->same设置为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;
    
    有人能帮我克服这个错误吗?如果你需要更多信息,请告诉我。提前谢谢

    编辑:这里是清理功能

    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;
    }
    
    char*SongSearch::clean(char*dirty)
    {
    线干净;
    int-iter=0;
    而(!isalnum(脏的[iter]))
    {
    iter++;
    }
    while(脏[iter]!='\0')
    {
    清洁+=肮脏[iter];
    iter++;
    }
    int backiter=clean.length()-1;
    而(!isalnum(干净的[backiter]))
    {
    清洁。擦除(背面,1);
    贝克特--;
    }
    字符c;
    
    对于(int i=0;i好的,我们确实需要查看其中一些的代码才能确定,但是这个bug告诉你的是:在某个时候,你分配给某个东西,它会覆盖或删除你的clean。因为y,你将它声明为char*,我猜你会将它用作指向一个字符数组的指针,一个数组s在两个不同的单词中被别名为两个“干净”指针。

    您需要将代码简化为显示问题的最小示例,然后发布该示例

    以下代码无法显示问题。将从您的代码中复制
    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;
    }
    

    问题可能是在
    clean
    中,返回
    clean.c_str()

    当函数退出时,
    clean
    不再存在,该指针值就不再有效。它不再保证指向任何东西,因此您很幸运能够像预期的那样“再次”看到字符串


    我怀疑发生的事情是,以前被
    clean
    中字符串
    clean
    的数据占用的内存已经被重新用于结构
    word
    ,但没有立即被覆盖。碰巧的是,用来保存第一个
    a
    的字节现在保存了
    相同的
    me的一部分因此,当您向
    节点->相同的
    写入空指针时,它的作用是向
    节点->清除
    指向的位置写入一个0字节。此后,它似乎指向一个空字符串。

    除了new和cout之外,这也可能是C

    其他一些阅读




    尝试以下替代方法(未编译的示例)

    #包括
    #包括
    #包括
    #包括
    typedef int-SongId;
    类词{
    内部位置;
    宋体id;
    单词*相同;
    单词*diff;
    公众:
    常量std::字符串字;
    const int size()const{return clean.length()};
    单词(常量标准::字符串和单词,常量整数位置=1739,常量整数id=0)
    :清洁
    ,位置(位置)
    ,id(id_)
    ,相同(空)
    ,差异(空)
    {
    
    coutWhat
    cleanse
    ?函数?发布它的代码。它是否返回本地字符数组?如果你做某事时受到伤害,不要这样做。停止使用char*、Word*,并使用std::strings和Word向量。重新考虑结构的名称-为什么它被称为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
    
    #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);
    }