请教各位大神,free释放空间错误

mk112255 2014-03-25 07:34:50
#include<stdio.h>
#include<malloc.h>
typedef struct niou
{
int info;
struct niou *next;
}HeadLink;
HeadLink *headcreat()
{
HeadLink *head,*p;
int i;
head=(struct niou *)malloc(sizeof(struct niou *));
head->next=NULL;
while(i!=0)
{
scanf("%d",&i);
if(i>0)
{
p=(struct niou *)malloc(sizeof(struct niou *));
p->info=i;
p->next=head->next ;
head->next=p;
}
}
getchar();
return head;
}
HeadLink print(HeadLink *head)
{
HeadLink *p;
p=head->next;
printf("dele before N0.\n");
while(p)
{
printf("%d ",p->info);
p=p->next;
}
}
HeadLink *delinsert(HeadLink *ha,HeadLink *hb,int i,int j,int len)
{
HeadLink *p,*q,*u;
int k=1;
if(i<1||len<1||j<1)
{
printf("error.\n");
// exit(0);
}
p=ha;
while(p&&k<i)
{
k++;
p=p->next;
}
if(k<i)
{
printf("%d is too big.\n",i);
// exit(0);
}
q=p->next;
k=1;
while(q&&k<len)
{
k++;
u=q;
q=q->next;
free(u);
}
if(k<len)
{
printf("%d too big.\n",len);
// exit(0);
}
p->next=q;
if(ha->next)
{
while(p->next)
p=p->next;
q=hb;
k=1;
while(q&&k<j-1)
{
k++;
q=q->next;
}
if(k<j)
{
printf("%d too big.\n",j);
// exit(0);
}
p->next=q->next;
q->next=ha->next;
}
return hb;
}
void main()
{
HeadLink *ha,*hb;
int i,j,len;
ha=headcreat();
hb=headcreat();
print(ha);
print(hb);
printf("input i,j and len:");
scanf("%d%d%d",&i,&j,&len);
hb=delinsert(ha,hb,i,j,len);
printf("\nafter delete :");
print(hb);
}
我在编译这个程序的时候,程序就是free(u);提示错误,但是又没有发现那里出了问题,请教各位大神
...全文
58 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zephyr3d 2014-03-25
  • 打赏
  • 举报
回复
head=(struct niou *)malloc(sizeof(struct niou *)) 你要分配niou大小的内存,而不是一个指针的大小,内存越界了 应改为head=(struct niou*)malloc(sizeof(struct niou))

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧