1

我正在尝试在 C 中实现链表。我使用了一个名为 NODE 的结构。出于某种原因,我Segmentation Error 11在调用函数时得到了 a 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 回答