c - 加载函数 trie 段错误

标签 c null segmentation-fault trie cs50

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

    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) != '\0')
       {
        //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。我有三种类型的节点:根节点、遍历节点(用于移动)和下一个节点。 (i.) 在分配节点之前,我是否可以将节点设置为空? (ii.) 另外,如果“next_node”节点在 if 语句内初始化并分配,我该如何释放该节点? node *next_node = malloc(sizeof(node)); (iii.) 如果我想将节点设置为全局变量,哪些应该是全局变量? (iv.) 最后,在哪里设置全局变量:在speller.c 的main 内部、main 外部,还是其他地方?问题很多,所以您不必回答所有问题,但如果您能回答已回答的问题,那就太好了!请指出我的代码中的任何其他特性。应该有很多。我会接受大多数答案。

最佳答案

段错误的原因是指针“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) != '\0')
   {

但是您没有移动 c 指针来指向读取的字符串中的下一个字符,因为该代码最终将执行无限 while 循环。 你可以尝试这样的事情-

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

关于c - 加载函数 trie 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052907/

相关文章:

c++ - 在使用Builder Pattern的构造函数中将指针类型的成员数据初始化为NULL

C strtok 和 strcpy

c - C语言编程时在ROM的特定地址写入特定值

c - 按升序和降序对列表进行排序

c - 返回字符串的函数不工作 - 段错误

c - 二叉树产生Segmentation Fault

Android 致命信号 11 (SIGSEGV) onFinish

c - 对 struct tm 的数组进行排序

swift - NSData(contentsOfURL : url) always returning nil

c# - 将空的 Textbox.Text 读取为 ""、null 或 String.Empty?