加载函数 trie 分段错误

Load function trie segmentation fault

我的加载函数不断出现段错误。

    bool load(const char *dictionary)
{
    //create a trie data type
   typedef struct node
   {
        bool is_word;
        struct node *children[27]; //this is a pointer too!  
   }node;

   //create a pointer to the root of the trie and never move this (use traversal *)
   node *root = malloc(sizeof(node));
   for(int i=0; i<27; i++)
   {
       //NULL point all indexes of root -> children
       root -> children[i] = NULL;
   }


   FILE *dptr = fopen(dictionary, "r");
   if(dptr == NULL)
   {
       printf("Could not open dictionary\n");
       return false;
   }



   char *c = NULL;


   //scan the file char by char until end and store it in c
   while(fscanf(dptr,"%s",c) != EOF)
   {
       //in the beginning of every word, make a traversal pointer copy of root so we can always refer back to root
       node *trav = root;

       //repeat for every word
       while ((*c) != '[=10=]')
       {
        //convert char into array index
       int alpha = (tolower(*c) - 97);

       //if array element is pointing to NULL, i.e. it hasn't been open yet,
        if(trav -> children[alpha] == NULL)
            {
            //then create a new node and point it with the previous pointer. 
            node *next_node = malloc(sizeof(node));
            trav -> children[alpha] = next_node; 

            //quit if malloc returns null
            if(next_node == NULL)
                {
                    printf("Could not open dictionary");
                    return false;
                }

            }

        else if (trav -> children[alpha] != NULL)
            {
            //if an already existing path, just go to it
            trav = trav -> children[alpha];
            }   
       }
        //a word is loaded. 
        trav -> is_word = true;
   }
   //success
   free(root);
   return true;
}

我检查了我在初始化期间是否正确地将新指针指向了 NULL。我有三种类型的节点:根节点、遍历节点(用于移动)和 next_node。 (i.) 我可以在 malloc 之前将节点设为空吗? (ii.) 此外,如果该节点在 if 语句中被初始化和分配,我如何释放 'next_node'? node *next_node = malloc(sizeof(node)); (iii.) 如果我想将节点设置为全局变量,哪些应该是全局的? (iv.) 最后,我在哪里设置全局变量:在 speller.c 的主体内、在其主体外,还是在其他地方?这是很多问题,所以您不必回答所有问题,但如果您能回答已回答的问题,那就太好了!请指出我的代码中的任何其他特性。应该有很多。我会接受大多数答案。

段错误的原因是您没有分配内存的指针"c"。

还有,在你的程序中-

//scan the file char by char until end and store it in c
while(fscanf(dptr,"%s",c) != EOF)

一旦你为指针c分配内存,c就会保存从文件字典中读取的单词。 在您的代码下方,您正在检查 '\0' 字符 -

   while ((*c) != '[=11=]')
   {

但是您没有将 c 指针移动到指向读取的字符串中的下一个字符,因此这段代码最终将执行无限 while 循环。 可以试试这样吗-

char *tmp;
tmp = c;
while ((*tmp) != '[=12=]')
{
     ......
     ......
     //Below in the loop at appropriate place
     tmp++;
}