free出错

tracyjk 2010-12-16 09:22:12
#include <stdio.h>

int getinfo(int num,char *buf,char *retval)
{
char *in = buf;
while(*in)
{
if(*in == '|')
{
num--;
if(0 == num)
break;
}
else
{
*retval = *in;
retval++;
in++;
}
}
}
main()
{
char a[]="02|03|04|05|06|0201|0202|";
char b[]="6605672251|6605672252|6605754283|6605672254|6605672255|6605672256|6605754287|";

char *excp_record,*cons_record;
excp_record = (char *)malloc(sizeof(a)+1);
cons_record = (char *)malloc(sizeof(b)+1);

excp_record = a;
cons_record = b;
int i;
char *temp,*type;
temp = (char *)malloc(5);
type = (char *)malloc(12);
for(i = 1; i < 15; i++)
{
memset(temp,0x00,12);
memset(type,0x00,6);
getinfo(i,excp_record,temp);
getinfo(i,cons_record,type);
printf("%s-%s\n",temp,type);
}

free(excp_record);
free(cons_record);
free(temp);
free(type);
return 0;
}


错误如下:
(gdb)
46 free(excp_record);
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x0092faa0 in free () from /lib/tls/libc.so.6

不明白为什么free会出错,麻烦哪个大牛告知下
...全文
481 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo364 2010-12-16
  • 打赏
  • 举报
回复
如下是报错的提示,应该是没加头文件,至于楼下说的问题,也应该注意。
D:\C project\3.c||In function 'main':|
D:\C project\3.c|28|warning: incompatible implicit declaration of built-in function 'malloc'|
D:\C project\3.c|39|warning: incompatible implicit declaration of built-in function 'memset'|
D:\C project\3.c|46|warning: incompatible implicit declaration of built-in function 'free'|
||=== Build finished: 0 errors, 3 warnings ===|

#include <stdio.h>

int getinfo(int num,char *buf,char *retval)
{
char *in = buf;
while(*in)
{
if(*in == '|')
{
num--;
if(0 == num)
{
break;
}
}
else
{
*retval = *in;
retval++;
in++;
}
}
}
main()
{
char a[]="02|03|04|05|06|0201|0202|";
char b[]="6605672251|6605672252|6605754283|6605672254|6605672255|6605672256|6605754287|";

char *excp_record,*cons_record;
excp_record = (char *)malloc(sizeof(a)+1);
cons_record = (char *)malloc(sizeof(b)+1);

excp_record = a;
cons_record = b;
int i;
char *temp,*type;
temp = (char *)malloc(5);
type = (char *)malloc(12);
for(i = 1; i < 15; i++)
{
memset(temp,0x00,12);
memset(type,0x00,6);
getinfo(i,excp_record,temp);
getinfo(i,cons_record,type);
printf("%s-%s\n",temp,type);
}

free(excp_record);
free(cons_record);
free(temp);
free(type);

system("pause");
return 0;
}
tracyjk 2010-12-16
  • 打赏
  • 举报
回复
解决了
THX
jokenit 2010-12-16
  • 打赏
  • 举报
回复
excp_record = a;
cons_record = b;

既然给“excp_record”和“cons_record”分配了内存,就不应该再把它们指向别的地方。后来free的是系统分配给a和b的空间,当然会出错。你这边应该是对它们进行赋值吧?可以用snprintf和memcpy等。
PG 2010-12-16
  • 打赏
  • 举报
回复
excp_record = a;
cons_record = b;
freefei 2010-12-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sujiewen 的回复:]

excp_record = a;
cons_record = b;
你不能这样做
[/Quote]
+1
bdmh 2010-12-16
  • 打赏
  • 举报
回复

excp_record = a;
cons_record = b;
换成
strcpy(excp_record,a);
strcpy(cons_record,b);

就想叫yoko 2010-12-16
  • 打赏
  • 举报
回复
+1[Quote=引用 5 楼 sujiewen 的回复:]
excp_record = a;
cons_record = b;
这里错了,你这样近是excp_record 指针指向a数组,你free数组a能不错吗?
memcpy(excp_record,a,size);
[/Quote]
AlanBruce 2010-12-16
  • 打赏
  • 举报
回复
蹲在楼下
bdmh 2010-12-16
  • 打赏
  • 举报
回复
excp_record 的内存已经被破坏,free时,已经不能正确访问当成malloc的内存了,跟踪一下看看哪里对其进行了改变
pengzhixi 2010-12-16
  • 打赏
  • 举报
回复
//excp_record = a;
// cons_record = b;

这两句注销吧,否则你分配的内存全部丢失了。
zhanghuayi_xiyou 2010-12-16
  • 打赏
  • 举报
回复
把一些对excp_record 操作忽略掉,看看做了什么:

char a[]="02|03|04|05|06|0201|0202|";
excp_record = (char *)malloc(sizeof(a)+1);
excp_record = a;
free (excp_record);

申请堆的数据空间,释放的栈的数据。
sujiewen 2010-12-16
  • 打赏
  • 举报
回复
excp_record = a;
cons_record = b;
这里错了,你这样近是excp_record 指针指向a数组,你free数组a能不错吗?
memcpy(excp_record,a,size);
T0Ols 2010-12-16
  • 打赏
  • 举报
回复
free函数的头文件是 malloc.h
然后我估计还是报错 但是错误不一样了 把错误在发出来
sujiewen 2010-12-16
  • 打赏
  • 举报
回复
excp_record = a;
cons_record = b;
你不能这样做
無_1024 2010-12-16
  • 打赏
  • 举报
回复

#include <malloc.h>
#include <string.h>

缺少这两个头文件
無_1024 2010-12-16
  • 打赏
  • 举报
回复
缺少头文件啊
#include <maloc.h>

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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