繁体   English   中英

尝试释放链接列表时,SIGABRT

[英]SIGABRT while attempting to free a linked list

我正在研究教授给我们准备的一些较旧的文章,为即将到来的考试做准备,但我遇到了这个问题。

我的任务是从结构如下的文本文件中读取信息:

[小数],[罗马数字(字符串)],[o或u(优化或未优化的罗马数字)]

几千行,然后使用十进制数作为关键字,将该信息存储在二叉搜索树中。 每个分支还必须包含该数目的出现次数以及遇到的各种罗马版本的列表,其中最优化的版本位于列表的顶部。 然后释放一切。

我的代码(在C中):

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

struct list //list node
{
  char *rom;
  struct list *next;
};

struct branch //main tree branch
{
  int count, dec;
  struct list *list;
  struct branch *right, *left;
};

struct list *insertnode(struct list *head, char *rom, char opt) //creates a head or creates and adds a new node to the end
{
  struct list *new;
  if(!head) //if !head, make one
    {
      new = malloc(sizeof(new));
      new->rom = malloc(sizeof(char)*(strlen(rom)+1));
      strcpy(new->rom, rom);
      new->next = NULL;
      return new;
    }
  if(opt == 'o') //if the roman form is optimized, put it in front of all the others
    {
      new = malloc(sizeof(new));
      new->rom = malloc(sizeof(char)*(strlen(rom)+1));
      strcpy(new->rom, rom);
      new->next = head;
      return new;
    }
  head->next = insertnode(head->next, rom, opt); //recursive insertions
  return head;
}

struct branch *insertbranch(struct branch *root, int dec, char *rom, char opt) //creates a root or creates and adds a new branch
{
  struct branch *new;
  if(!root) //if !root, make a root...
    {
      new = malloc(sizeof(new));
      new->list = insertnode(new->list, rom, opt);
      new->dec = dec;
      new->count = 1;
      new->right=new->left=NULL;
      return new;
    }
  if(dec<root->dec) root->left = insertbranch(root->left, dec, rom, opt); //branch on the left, recursive
  else if(dec>root->dec) root->right = insertbranch(root->right, dec, rom, opt); //branch on the right, recursive
  else //if there already is such a branch, increase its count
    {
      root->count += 1;
      root->list = insertnode(root->list, rom, opt);
    }
  return root;
}

void freelist(struct list *head) //frees list 'head'
{
  struct list *tmp;
  while(head)
    {
      tmp = head;
      head = head->next;
      free(tmp->rom);
      free(tmp); // <- OFFENDING LINE
    }
  return;
}

void freetree(struct branch *root) //frees tree 'root'
{
  if(!root) return;
  freetree(root->left); //recursive!
  freetree(root->right); //and on the right
  free(root->right);
  free(root->left);
  freelist(root->list);
  free(root);
  return;
}

int main()
{
  struct branch *root;
  struct list *list;
  int dec, i=1, n;
  char rom[30], opt;
  FILE *file = fopen("rom.csv", "r");
  if(!file) //is the file even there?
    return 1;

  while(fscanf(file, "%d,%[^,],%c\n", &dec, rom, &opt)==3) //go through the file and fill tree
    root = insertbranch(root, dec, rom, opt);

  freetree(root);
  printf("Goodbye!\n");
  return 0;
}

调试后,我发现了问题所在:在“ freelist”函数中,当它到达命令“ free(tmp)”时,程序将中止。 我不知道原因可能是什么。 我什至检查以确保节点头存在。

谢谢你的帮助!

您没有在insertnode()insertbranch()分配正确的内存量。

您需要替换以下内容:

new = malloc(sizeof(new));

带有:

new = malloc(sizeof(*new));

这是因为new是一个指针。 使用malloc(sizeof(new)) ,您仅分配空间来存储指针,而不分配必要的空间来存储结构的内容。

这是这些功能的正确版本:

struct list *insertnode(struct list *head, char *rom, char opt) //creates a head or creates and adds a new node to the end
{
    struct list *new;
    if(!head) //if !head, make one
    {
        new = malloc(sizeof(*new));
        new->rom = malloc(sizeof(char)*(strlen(rom)+1));
        strcpy(new->rom, rom);
        new->next = NULL;
        return new;
    }
    if(opt == 'o') //if the roman form is optimized, put it in front of all the others
    {
        new = malloc(sizeof(*new));
        new->rom = malloc(sizeof(char)*(strlen(rom)+1));
        strcpy(new->rom, rom);
        new->next = head;
        return new;
    }
    head->next = insertnode(head->next, rom, opt); //recursive insertions
    return head;
}

struct branch *insertbranch(struct branch *root, int dec, char *rom, char opt) //creates a root or creates and adds a new branch
{
    struct branch *new;
    if(!root) //if !root, make a root...
    {
        new = malloc(sizeof(*new));
        new->list = insertnode(new->list, rom, opt);
        new->dec = dec;
        new->count = 1;
        new->right=new->left=NULL;
        return new;
    }
    if(dec<root->dec) root->left = insertbranch(root->left, dec, rom, opt); //branch on the left, recursive
    else if(dec>root->dec) root->right = insertbranch(root->right, dec, rom, opt); //branch on the right, recursive
    else //if there already is such a branch, increase its count
    {
        root->count += 1;
        root->list = insertnode(root->list, rom, opt);
    }
    return root;
}

我没有仔细研究每一行代码,但是看起来几乎是正确的。 从我的快速观察中,您唯一的错误是分配的内存少于您使用的内存。 过去分配的内存写入的结果是不可预测的,并且可以在程序的较后部分表现出来(例如释放内存时)。 这就是为什么未定义的行为很难调试的原因:)

我发现了问题。 在对freetree进行递归调用之后,我尝试再次以free(root->left)free(root->right)的形式释放相同的内存。 我现在觉得有点傻。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM