循环时链表中的分段错误?

时间:2018-11-18 13:17:25

标签: c

我正在尝试在C中设置一个图形。我使用用户输入尝试了该图形,它可以完美运行。但是,我试图实现从文件读取。最后的else语句是错误的出处,因为当我注释掉它时,它可以毫无问题地进行编译。我已经对我认为存在问题的区块发表了评论。请让我知道这个问题是否还有其他需要。

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node* next;
};

//int counter and mainVertex would be used to determine if graph is connected.

// void graphConnection(){
// 
// 
// 
// 
// 
// 
// }


char* deblank(char* input)
{
    int i,j;
    char *output=input;
    for (i = 0, j = 0; i<strlen(input); i++,j++)
    {
        if (input[i]!=' ')
            output[j]=input[i];
        else
            j--;
    }
    output[j]=0;
    return output;
}


struct node *G[1000];
int counter = 0;
char *mainVertex;

void readingEachLine(){

  FILE * fp;
  char * line = NULL;
  size_t len = 0;
  ssize_t read;

  //Read file and exit if fail
  fp = fopen("test.txt", "r");
  if (fp == NULL)
      exit(EXIT_FAILURE);

  while ((read = getline(&line, &len, fp)) != -1) {
    line = deblank(line);
    int i = 0;
    struct node* cursor = malloc(sizeof(struct node));
    struct node* secondcursor = malloc(sizeof(struct node));
    struct node* tempitem;
    while(line[i] != '\n'){

      //If its the first of the line look into the array and set struct cursor to the corresponding
      //array position

      if (i == 0){
        mainVertex[counter] = line[0];
        int convertor = line[i] - '0';
        cursor = G[convertor];
        counter++;
      }
      //if its not the first, then set a struct with that number as data

      else{
        tempitem = malloc(sizeof(struct node));
        int convertor = line[i] - '0';
        tempitem->data = convertor;
        tempitem->next = NULL;
      }
      //if there is no element connected to the struct in array, connect the tempitem

      if (cursor->next == NULL){
        cursor->next = tempitem;
      }
      //If there are already connected elements, loop until the end of the linked list
      //and append the tempitem
      //ERROR: I GET SEGMENTATION FAULT FROM HERE. TRIED AFTER COMMENTING IT OUT

      else{
        secondcursor = cursor;
        while(secondcursor->next != NULL){
          secondcursor = secondcursor->next;
        }
        secondcursor->next = tempitem;
      }
      i++;
    }
    printf("\n");
  }
}

int main(void){
  for (int i = 1; i < 1000; i++)
  {
      G[i]= malloc(sizeof(struct node));
      G[i]->data = i;
      G[i]->next = NULL;
  }
  readingEachLine();
}

编辑:这是文本文件的外观:

1 3 4
2 4
3 1 4
4 2 1 3

1 个答案:

答案 0 :(得分:1)

您的代码有几个错误的选择:

  • 显然,您最多可以有1,000个节点。您有一个数组G,其中包含指向链接列表的1000个头指针。开始时不要为所有1,000个节点分配内存。最初,所有列表都是空的,而空链接列表是没有节点且头为NULL的列表。
  • 在您的示例中,cursor用于迭代已经存在的指针,因此请不要为其分配内存。如果您有这样的代码:

    struct node *p = malloc(...);
    
    // next use of p:
    p = other_node;
    

    您不应该分配。您将覆盖p并失去分配内存的句柄。并非所有指针都必须使用malloc初始化;仅在创建节点时分配。

  • 如果您有9个以上的节点,那么您要删除一行中的所有空格然后解析单个数字的想法将失败。 (但是您可以容纳1,000个节点。)不要尝试自己解析数字。有相应的库函数,例如strtol
  • 目前尚不清楚mainVertex应该是什么。分配给它时,只能使用一次。您将其视为数组,但它是初始化为NULL的全局指针。取消引用时,您会得到不确定的行为,这可能是细分错误的来源。

这是一个执行您想要做的程序的程序。 (为简单起见,它总是在头插入节点,并且应该进行更多的分配检查。)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum {
    maxNodes = 1000
};

struct node{
    int data;
    struct node* next;
};

struct node *G[maxNodes];
size_t nnode = 0;

int read_graph(const char *fn)
{
    FILE * fp;
    char * line = NULL;
    size_t len = 0;

    fp = fopen(fn, "r");
    if (fp == NULL) return -1;

    while (getline(&line, &len, fp) != -1) {
        char *p;
        char *end;
        int id;
        int n;

        id = strtol(line, &end, 10);
        if (end == line) continue;

        if (id < 1 || id > maxNodes) break;

        if (id > nnode) nnode = id;
        id--;

        p = end;
        n = strtol(p, &end, 10);

        while (p != end) {
            struct node *nnew = malloc(sizeof(*nnew));

            nnew->data = n - 1;
            nnew->next = G[id];
            G[id] = nnew;

            p = end;
            n = strtol(p, &end, 10);
        }
    }

    fclose(fp);
    free(line);

    return 0;
}

int main(void)
{
    if (read_graph("test.txt") < 0) {
        fprintf(stderr, "Couldn't gread raph.\n");
        exit(1);
    }

    for (int i = 0; i < nnode; i++) {
        struct node *p = G[i];

        if (p) {
            printf("%d:", i + 1);

            for (; p; p = p->next) {
                printf(" %d", p->data + 1);
            }

            puts("");
        }
    }

    for (int i = 0; i < nnode; i++) {
        struct node *p = G[i];

        while (p) {
            struct node *old = p;

            p = p->next;
            free(old);
        }
    }

    return 0;
}