1

C でリンク リストを実装しようとしています。NODE という構造を使用しました。何らかの理由でSegmentation Error 11、関数を呼び出すとが取得されますcreateList()。私は自分のコードをデバッグしましたが、ここでエラーが発生することを確信しています:

/* 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 をテストし、head を 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);
}
4

2 に答える 2

7

はい、これは問題になります。

  /* 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;
}
于 2013-11-05T02:52:48.080 に答える
1

頭が割り当てられていないように見えます

于 2013-11-05T02:53:33.140 に答える