簡體   English   中英

分段錯誤:11-C鏈表

[英]Segmentation Fault: 11 - C Linked-List

我試圖在C中實現鏈表。我使用了一種稱為NODE的結構。 出於某些原因,當我調用函數createList()時會遇到Segmentation Error 11 我調試了我的代碼,並且我很確定該錯誤發生在這里:

/* 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並將頭發送給add(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);
}

是的,這將是一個問題。

  /* 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;
}

在我看來,人頭沒有分配

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM