teio931226 2023-03-15 00:20 采纳率: 100%
浏览 27
已结题

关于#链表#的问题:刚学到单链表,发现free函数一用就报错:Trace/breakpointtrap,请问是什么原因呢😭#include <stdio.h>(语言-c语言)

刚学到单链表,发现free函数一用就报错:Trace/breakpoint trap,请问是什么原因呢😭


#include <stdio.h>
#include <stdlib.h>
struct node{
    int c;//系数
    int e;//指数
    struct node *next=NULL;
};
void insert(int c,int e,node *head){
    if(c==0){
        return;
    }
    node *q;
    q=(struct node*) malloc (sizeof(node));
    q->c=c;
    q->e=e;
    q->next=head->next;
    head->next=q;
    //free(q)报错?
}
void read(node *head){
    head=(struct node*) malloc (sizeof(node));
    head->next=NULL;
    int c,e;
    while(1){
        scanf("%d %d",&c,&e);
        insert(c,e,head);
        if(c==0){
            return;
        }
        else{
            head=head->next;
        }
    }
}//读入多项式
int main(){
    node *f,*g;
    read(f);
    read(g);
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2023-03-15 04:46
    关注

    在这free的话,下个节点增加就崩溃了。q是有效节点,不能free

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭