繁体   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