为什么会出现此段错误

为什么会出现此段错误

Program received signal SIGSEGV, Segmentation fault.
0x08048637 in insertWordDictR (string=0xbf87e7b6 "a", child=0x20f88,
    isTerminal=0x20f8c) at dictionary.c:54
54                        *isTerminal = False;

在头文件里
typedef enum {False, True} bool;

下面是相关的代码行:
dictLink insertWordDictR(char *string, dictLink child, bool* isTerminal){
  dictLink newWord;

  if(isTerminal != NULL){
    if(string[0] == '\0'){
          *isTerminal = True;
          return NULL;
    }else{
          *isTerminal = False;
    }
  }


  if (child == NULL){
      newWord = malloc(sizeof (struct dictEdge));
      newWord->thisChar = string[0];
      newWord->child = insertWordDictR(string + 1, newWord, &(newWord->isTerminal));
      newWord->sibling = NULL;
  }else{
      newWord = insertIntoSiblingList(string, child);

  }

  return newWord;
}

这一行是我如何调用这个函数的一个例子
  newWord->child = insertWordDictR(string + 1, newWord, &(newWord->isTerminal));

All files can be found here,还有一个名为“testing”的输入文件。

最佳答案

在此序列中,您将从未初始化的指针中获取字段的地址,如插入的注释所示:

dictLink result;
if(sibling == NULL){
      result = malloc(sizeof (struct dictEdge));
      result->thisChar = string[0];
      result->sibling = NULL;
      result->child = insertWordDictR(string + 1, result, &(result->isTerminal));

}else if(string[0] < sibling->thisChar){
      result = malloc(sizeof (struct dictEdge));
      result->thisChar = string[0];
      result->sibling = sibling;
      result->child = insertWordDictR(string + 1, result, &(result->isTerminal));

  }else if(string[0] == sibling->thisChar){
      /* "result" has not been initialized, &(result->isTerminal) is an invalid pointer */
      sibling->child = insertWordDictR(string + 1, result, &(result->isTerminal));
    result = sibling;

关于c - 为什么会出现此段错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6081753/

10-12 22:22