2015-05-08 108 views
1

我有一個結構鏈表,插入/打印功能如下,鏈表段錯誤/打印

typedef struct node{ 
    int size; 
    int day; 
    int year; 
    char* month; 
    char* fileName; 
    struct node *next; 
}node; 

void insert(node ** head, int size, int year, int day, char* month, char* fileName){ 
    node * newNode = (node*)malloc(sizeof(node)); 
    newNode->size = size; 
    newNode->fileName = fileName; 
    newNode->day = day; 
    newNode->month = month; 
    newNode->year = year; 
    newNode->next = *head; 
    *head = newNode; 
    puts("insert called"); 
    //printf("head is %s\n", newNode->fileName); 
} 
void print_list(node * head) { 
    node * current = head; 
    while (current != NULL){ 
     puts("got here"); 
     printf("head %s\n", current->fileName); 
     current = current->next; 
     printf("next filename %s\n", current->fileName); 
    } 
} 

當我打電話插入功能,它打印「插入所謂的」消息作爲預期我調用該函數的次數。所以我的猜測是插入功能不是問題。

我的問題是,我得到一個段錯誤的打印功能打印前幾個節點列表後(它工作正常的前兩個節點!)

這裏是代碼的其餘部分,其中插入功能被調用時, 我不認爲這是她的一個問題,但我可能是錯誤:

所有調用鏈表功能是向底部

void p5aFlag(char* pathName){ 
    DIR* d; 
    struct dirent *dir; 
    struct stat s; 
    struct tm *tp; 
    char* fileName; 
    char temp2[strlen(pathName)]; 
    char * month; 
    time_t t; 
    d = opendir(pathName); 
    int year; 
    int day; 
    int size; 
    node *head = NULL; 
    int flag = 0; 

    if(d){ //While there are more files in the directory, keep reading them off 
     while ((dir = readdir(d)) != NULL){ 
      fileName = dir->d_name; //store the name of the directory that was just read in fileName 
      strcpy(temp2,pathName); // copy the directory name into temp2 

      if(strcmp(fileName, ".") != 0 && strcmp(fileName, "..") != 0){ // get file info if the file isn't "." or ".." 
       strncat(temp2,"/",2); // prepare the path to lead to another file 
       strncat(temp2, fileName, 15); // append the fileName to the path, now we can open this file 
       if(stat(temp2, &s) == -1){ // if we cannot get information for this file, print error message and exit 
        fprintf(stderr, "couldnt get file info for %s\n", fileName); 
       } 
       t = s.st_mtime; //store the time that the file was last modified in t 
       tp = localtime(&t); // convert to local time 
       switch(tp->tm_mon){ // switch to change months from [0-11] to [Jan-Dec] 
        case 0: 
        month = "Jan"; 
        break; 
        case 1: 
        month = "Feb"; 
        break; 
        case 2: 
        month = "Mar"; 
        break; 
        case 3: 
        month = "Apr"; 
        break; 
        case 4: 
        month = "May"; 
        break; 
        case 5: 
        month = "Jun"; 
        break; 
        case 6: 
        month = "Jul"; 
        break; 
        case 7: 
        month = "Aug"; 
        break; 
        case 8: 
        month = "Sep"; 
        break; 
        case 9: 
        month = "Oct"; 
        break; 
        case 10: 
        month = "Nov"; 
        break; 
        case 11: 
        month = "Dec"; 
        break; 
        default: 
        break; 
       } 
       flag = 1; 
      } 
      if(flag == 1){ 
       year = 1900 + tp->tm_year; 
       size = s.st_size; 
       day = tp->tm_mday; 
       insert(&head, size, year, day, month, fileName); 
       flag = 0; 
      } 

      temp2[0]= '\0'; 
      flag = 0; 
     } 
     //search(head, ".emacs"); 
     print_list(head); 
     closedir(d); // close the directory 
    } 
} 

我不知道爲什麼發生這種情況。任何幫助將不勝感激。

回答

1

打印的最後一個元素時,您將獲得一個段錯誤,因爲你設置的電流到當前>旁邊,但不檢查它是否是NULL打印下文件名前:

void print_list(node * head) { 
    node * current = head; 
    while (current != NULL){ 
     puts("got here"); 
     printf("head %s\n", current->fileName); 
     current = current->next; 
     if(current != NULL) /// add this check 
      printf("next filename %s\n", current->fileName); 
    } 
} 

你也有一個無意義分配和內存泄漏的位置:

node *head = (struct node *) malloc(sizeof(node)); 
head = NULL; 

您分配一個節點,但然後通過設置頭NULL失去對它的任何引用。你可以只初始化爲NULL這樣的:

node *head = NULL; 
+1

和一般(沒有施展的malloc)注:'節點*頭=的malloc(*的sizeof頭);'就足夠了。 (儘管設置爲NULL的問題)@samgak,這不是針對你,只是一個閱讀的將軍。好答案。 –

+0

感謝您的輸入,但是我已經做出了更改,現在我只是在打印頭後出現總線錯誤。 –

+1

這不會只是通過檢查NULL指針而引起的,這是您的代碼中的一個單獨問題。你應該做的一件事是將文件名字符串的副本存儲在節點中,而不是使用從readdir返回的指針,該指針指向每次調用readdir時都會被覆蓋的字符串。 – samgak