2015-11-05 144 views
1

我正在編寫一個數據結構,其中包含一組兩個鏈接列表,堆疊在一起。當試圖在我的測試工具中初始化集合時,出現分段錯誤。我已經評論過所有的價值評估者來測試我是否可以自己弄清楚這個錯誤,但我不能。問題初始化C中的雙重堆棧鏈接列表

原型的init方法:

測試工具:

int 
main() 
{ 
    list the_list; 
    int used = 0; 
    int values[MAX_VALUES]; 
    char input[LINE_LEN]; 
    char command; 
    int argument; 
    int num_found; 
    bool result; 
    set_t lower; 
    set_t upper; 
    the_list->lower = lower; 
    the_list->upper = upper; 

input[0] = '\0'; 
    input[LINE_LEN-1] = '\0'; 

    fgets(input, LINE_LEN, stdin); 
    while (*input != 'q') { 
    num_found = sscanf(input, "%c %d", &command, &argument); 

    if (num_found > 0) { 
     switch (command) { 
     case 'i': 
      printf ("Request to initialize the set\n"); 
      if (num_found == 1) { 
      result = set_init(&the_list); 
      } else { 
      result = set_init(NULL); 
      } 
      printf ("Returned as %d\n", result); 
      break; 


                 34,0-1   8% 

Init方法:

bool 
set_init(list *the_list) 
{ 
    bool initialized = false; 
    if (the_list !=NULL) { 
    /* We have space to initialize. */ 

    the_list->lower->set_size = 0; 
    /* the_list->lower->head = NULL; 
    the_list->lower->tail = NULL; 
    the_list->lower->set_level = 1; 
    the_list->lower->ready = true; 
    the_list->upper->set_size = 0; 
    the_list->upper->head = NULL; 
    the_list->upper->tail = NULL; 
    the_list->upper->set_level = 2; 
    the_list->upper->ready = true;*/ 
    initialized = true; 
    } 
    return initialized; 
    } 

而且我對我的設置結構的定義,鏈表和節點結構:

typedef struct _set_node_t { 
     test_type_t *data; 
     struct _set_node_t *next; 
     struct _set_node_t *below; 
} set_node_t; 

/* the set itself keeps track of the head and the tail of the linked list */ 
typedef struct { 

     int set_size; 
     bool ready; 
     set_node_t *head; 
     set_node_t *tail; 
     int set_level; 
} set_t; 

typedef struct { 
     set_t *lower; 
     set_t *upper; 
}list; 
+1

你的意思'如果(i == 1)'? – mihai

回答

0

可以在此處崩潰的唯一事情是這一行:

the_list->lower->set_size = 0; 

無論the_list或the_list->下必須未初始化或NULL或者指向無效或無法使用的內存。

編輯:是啊,這行會崩潰,因爲你不初始化the_list.lower:

result = set_init(&the_list); 

而此行會崩潰,因爲你傳遞NULL:

result = set_init(NULL); 
+0

我該如何解決這個問題?我嘗試設置整個集合,並將下部和上部鏈接列表設置爲null,但它仍然發送了分段錯誤 –

+0

您需要提供調用set_init() – kcraigie

+0

ok的代碼編輯併發布原型和測試用具,其中set_init )被稱爲 –