2013-11-05 141 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

看来,我认为头未分配