2013-11-05 103 views
1

我想在C中實現鏈表。我使用了一個名爲NODE的結構。出於某種原因,當我調用函數createList()時,我得到Segmentation Error 11。我調試我的代碼,我非常有信心,在這裏發生了錯誤:分割錯誤:11 - C鏈接列表

/* Write into node */ 
strcpy(head->username, cur_username); 
strcpy(head->password, cur_pw); 
strcpy(head->type, cur_type); 
printf("%s %s %s\n", head->username, head->password, head->type); 

我認爲錯誤是因爲內存是分配不正確。但是我爲每個節點值初始化了一個50個字符的數組,所以程序不應該自己管理內存?

char *filename = "password.csv"; 
int n_users = 0; 

struct NODE { 
    char username[50]; 
    char password[50]; 
    char type[50]; 
    struct NODE *next; 
} *head=NULL, *curr=NULL; 

/* Global placeholders so they can be used by other both add() and creatList() */ 
char line[50], *cur_username, *cur_pw, *cur_type; 
char linecopy[50]; 

void createList(void) { 
    printf("Creating list\n"); 

    bool success; 

    /* Open the file in read-only and copy content into array */ 

    /* File pointer */ 
    FILE *f; 

    /* Node pointer */ 
    struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE)); 
    if (NULL == ptr) { 
    printf("\nError: was unable to initialize password validation information!! \n"); 
    return; 
    } 

    head = curr = ptr; 

    /* Open the file in read */ 
    f = fopen("password.csv", "r"); 

    /* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */ 
    if (f != NULL) { 
    /* Read the file line by line */ 
    while(fgets(line, 49, f) != NULL) { 

     struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE)); 

     /* Copy line into linecopy because strtok is destructive */ 
     strcpy(linecopy, line); 

     /* Extract the ids from linecopy */ 
     cur_username = strtok(linecopy, ",");; 
     cur_pw = strtok(NULL, ","); 
     cur_type = strtok(NULL, ","); 

     if(head==NULL) 
     add(head); 
     else 
     add(p); 

     if (!success) { 
     printf("\nError: was unable to initialize password validation information!! \n"); 
     return; 
     } 
    } 
    } 

    /* Close file */ 
    fclose(f); 
} 

** HEAD是NULL **

雖然我沒有任何段錯誤了,我的頭不會被初始化......我讀從文件的輸入,並創建一個節點文件的每一行。因此,我正在測試head == NULL併發送頭添加(struct * NODE)。不應該初始化頭部?

struct NODE { 
    char username[50]; 
    char password[50]; 
    char type[50]; 
    struct NODE *next; 
} *head=NULL, *curr=NULL; 

/* Global placeholders so they can be used by other both add() and creatList() */ 
char line[50], *cur_username, *cur_pw, *cur_type; 
char linecopy[50]; 

void createList(void) { 
    printf("Creating list\n"); 

    bool success; 

    /* Open the file in read-only and copy content into array */ 

    /* File pointer */ 
    FILE *f; 

    /* Node pointer */ 
    struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE)); 
    if (NULL == ptr) { 
    printf("\nError: was unable to initialize password validation information!! \n"); 
    return; 
    } 

    head = curr = ptr; 

    /* Open the file in read */ 
    f = fopen("password.csv", "r"); 

    /* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */ 
    if (f != NULL) { 
    /* Read the file line by line */ 
    while(fgets(line, 49, f) != NULL) { 

     struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE)); 

     /* Copy line into linecopy because strtok is destructive */ 
     strcpy(linecopy, line); 

     /* Extract the ids from linecopy */ 
     cur_username = strtok(linecopy, ",");; 
     cur_pw = strtok(NULL, ","); 
     cur_type = strtok(NULL, ","); 

     if (head == NULL) 
     success = add(head); 
     else 
     success = add(p); 

     if (!success) { 
     printf("\nError: was unable to initialize password validation information!! \n"); 
     return; 
     } 
    } 
    } 

    /* Close file */ 
    fclose(f); 
} 

回答

6

是的,這將是一個問題。

/* Is list empty? */ 
    if (head == NULL) { 

    /* Write into node */ 
    strcpy(head->username, cur_username); 
    strcpy(head->password, cur_pw); 
    strcpy(head->type, cur_type); 
    printf("%s %s %s\n", head->username, head->password, head->type); 

    } 

解引用NULL通常會導致分段錯誤。

在這種情況下,您總是需要分配一個新節點。試試這個:

struct NODE *node = malloc(sizeof(struct NODE)); 

if (node != NULL) 
{ 
    strcpy(node->username, cur_username); 
    strcpy(node->password, cur_pw); 
    strcpy(node->type, cur_type); 

    /* push node onto list head */ 
    node->next = head; 
    head = node; 
} 
+0

哦,我明白了!我以爲頭是自動初始化的!所以我加了'struct NODE * ptr =(struct NODE *)malloc(sizeof(struct NODE));'然後'head = ptr',它起作用了!謝謝! –

+1

我會說「dereferencing NULL」,因爲這是什麼導致段錯誤。 – vanza

+0

好點,@vanza。糾正了我的措辭=) – paddy

1

看來,我認爲頭未分配