【问题标题】:C: Segmentation fault when trying to create a list using mallocC:尝试使用 malloc 创建列表时出现分段错误
【发布时间】:2015-01-20 16:28:47
【问题描述】:

我正在尝试使用 malloc 创建一个列表,程序获取用户输入的整数,并在用户输入 0 时退出。但是我遇到了 Segmentation fault (core dumped) 错误,我没有没能发现问题。我尝试过的事情包括添加“liberar”方法来释放内存,但它也不起作用。谢谢!

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

struct list1 {
   int val;
   struct list1 * sig;
};

typedef struct list1 i;

void main() {
   i * aux, * cabeza;
   int entrada;

   cabeza = NULL;

   while(1) {
      aux = (i*)malloc(sizeof(i));
      scanf("%d\n",entrada);
      if(entrada==0){
          exit(0);
      }
      aux->val = entrada;
      aux->sig  = cabeza;
      cabeza = aux;
      liberar(cabeza);
   }

   aux = cabeza;

   while(aux) {
      printf("%d\n", aux->val);
      aux = aux->sig ;
   }
}

int liberar(struct list1* cabez)
{   
    struct list1 *temp;
    while (cabez != NULL)
    {
       temp = cabez;
       cabez = cabez->sig;
       free(temp);
    }

}

【问题讨论】:

  • scanf("%d\n",entrada); --> scanf("%d", &amp;entrada);i * cabeza = NULL; cabeza 需要初始化。和liberar(cabeza); 删除。
  • i 是一个糟糕的类型别名名称...
  • 你为 aux 分配空间,将 aux 分配给 cabeza(这些只是指针,释放 cabeza(和 aux)指向的空间,然后再次使用 aux,即使它指向的内容已被释放. 那是行不通的
  • 1) 英文变量名更容易理解:entrada == input?卡贝萨 == ??? 2) malloc() 可能会失败。你必须检查结果。 3)您在知道需要它之前为 aux 分配列表元素。在 (entrada==0) 的情况下,您将泄漏内存(假设稍后您执行 exit(0) 以外的其他操作)
  • @BLUEPIXY 是的!那是错误,我只是添加了&并删除了方法,谢谢!

标签: c list segmentation-fault malloc


【解决方案1】:

纠正来自 cmets 的所有内容(以及一些未说明的内容),您将获得以下来源:

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

typedef struct List {
    int val;
    struct List * next;
} List;

void list_free(List * list)
{   
    while (list != NULL)
    {
        List *temp = list;
        list = list->next;
        free(temp);
    }
}

int main() {
    List * aux, * result;
    int input;

    result = NULL;

    while(1) {
        scanf("%d",&input);
        if(input == 0){
            break;
        }
        aux = (List *)malloc(sizeof(List));
        assert(aux != NULL);
        aux->val = input;
        aux->next  = result;
        result = aux;
    }

    aux = result;

    printf("Result =\n");

    while(aux) {
        printf("%d\n", aux->val);
        aux = aux->next;
    }

    list_free(result);

    return 0;
}

【讨论】:

  • 你可以把事情收紧(但这工作正常)。例如List *result = NULL;,而不是将声明和初始化分开。变量aux 应该在while (1) 循环中声明。此外,该循环应该是while (scanf("%d", &amp;input) == 1),没有测试并在循环内中断。结果打印代码可能是for (List *aux = result; aux != NULL; aux = aux-&gt;next) { printf("%d\n", aux-&gt;val); },再次限制了第二个aux 变量的范围。 C 标准总是在#include 和标题名称之间显示一个空格;你也应该这样做。
猜你喜欢
  • 2018-09-24
  • 2021-08-05
  • 2021-12-10
  • 2017-06-06
  • 2013-02-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多