c FILE 程序错误:双重释放或损坏
这是我的程序,它需要 10 个整数并找出偶数或奇数并将它们存储在各自的文件中。我使用 gcc。
#include<stdio.h>
int main()
{
int a,i;
FILE *fp1,*fp2,*fp3;
fp1=fopen("data","w");
for(i=0;i<10;i++)
{
scanf("%d",&a);
putw(a,fp1);
}
fclose(fp1);
fp1=fopen("data","r");
fp2=fopen("even","w");
fp3=fopen("odd","w");
while((a=getw(fp1))!=EOF)
if(a%2==0)
putw(a,fp2);
else
putw(a,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp1);
return 0;
}
我在输入 10 个值后得到这个,
*** glibc detected *** ./a.out: double free or corruption (!prev): 0x09b79008 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x17c501]
/lib/libc.so.6(+0x6dd70)[0x17dd70]
/lib/libc.so.6(cfree+0x6d)[0x180e5d]
/lib/libc.so.6(fclose+0x14a)[0x16c81a]
./a.out[0x80485e4]
/lib/libc.so.6(__libc_start_main+0xe7)[0x126ce7]
./a.out[0x8048421]
======= Memory map: ========
00110000-00267000 r-xp 00000000 08:04 686829 /lib/libc-2.12.1.so
00267000-00268000 ---p 00157000 08:04 686829 /lib/libc-2.12.1.so
00268000-0026a000 r--p 00157000 08:04 686829 /lib/libc-2.12.1.so
0026a000-0026b000 rw-p 00159000 08:04 686829 /lib/libc-2.12.1.so
0026b000-0026e000 rw-p 00000000 00:00 0
007b2000-007ce000 r-xp 00000000 08:04 686805 /lib/ld-2.12.1.so
007ce000-007cf000 r--p 0001b000 08:04 686805 /lib/ld-2.12.1.so
007cf000-007d0000 rw-p 0001c000 08:04 686805 /lib/ld-2.12.1.so
009cc000-009cd000 r-xp 00000000 00:00 0 [vdso]
00a2c000-00a46000 r-xp 00000000 08:04 686863 /lib/libgcc_s.so.1
00a46000-00a47000 r--p 00019000 08:04 686863 /lib/libgcc_s.so.1
00a47000-00a48000 rw-p 0001a000 08:04 686863 /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
08049000-0804a000 r--p 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
0804a000-0804b000 rw-p 00001000 08:04 753598 /home/bobby/Desktop/c/a.out
09b79000-09b9a000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77df000-b77e0000 rw-p 00000000 00:00 0
b77ea000-b77eb000 rw-p 00000000 00:00 0
b77ed000-b77f0000 rw-p 00000000 00:00 0
bfed2000-bfef3000 rw-p 00000000 00:00 0 [stack]
Aborted
我想知道它是什么。这是我第一次遇到此类错误。我想了解该错误并找出我的程序出了什么问题。
This my program where it takes 10 integers and finds out if even or odd and stores them in respective files.I use gcc.
#include<stdio.h>
int main()
{
int a,i;
FILE *fp1,*fp2,*fp3;
fp1=fopen("data","w");
for(i=0;i<10;i++)
{
scanf("%d",&a);
putw(a,fp1);
}
fclose(fp1);
fp1=fopen("data","r");
fp2=fopen("even","w");
fp3=fopen("odd","w");
while((a=getw(fp1))!=EOF)
if(a%2==0)
putw(a,fp2);
else
putw(a,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp1);
return 0;
}
I get this after entering the 10 values
*** glibc detected *** ./a.out: double free or corruption (!prev): 0x09b79008 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x17c501]
/lib/libc.so.6(+0x6dd70)[0x17dd70]
/lib/libc.so.6(cfree+0x6d)[0x180e5d]
/lib/libc.so.6(fclose+0x14a)[0x16c81a]
./a.out[0x80485e4]
/lib/libc.so.6(__libc_start_main+0xe7)[0x126ce7]
./a.out[0x8048421]
======= Memory map: ========
00110000-00267000 r-xp 00000000 08:04 686829 /lib/libc-2.12.1.so
00267000-00268000 ---p 00157000 08:04 686829 /lib/libc-2.12.1.so
00268000-0026a000 r--p 00157000 08:04 686829 /lib/libc-2.12.1.so
0026a000-0026b000 rw-p 00159000 08:04 686829 /lib/libc-2.12.1.so
0026b000-0026e000 rw-p 00000000 00:00 0
007b2000-007ce000 r-xp 00000000 08:04 686805 /lib/ld-2.12.1.so
007ce000-007cf000 r--p 0001b000 08:04 686805 /lib/ld-2.12.1.so
007cf000-007d0000 rw-p 0001c000 08:04 686805 /lib/ld-2.12.1.so
009cc000-009cd000 r-xp 00000000 00:00 0 [vdso]
00a2c000-00a46000 r-xp 00000000 08:04 686863 /lib/libgcc_s.so.1
00a46000-00a47000 r--p 00019000 08:04 686863 /lib/libgcc_s.so.1
00a47000-00a48000 rw-p 0001a000 08:04 686863 /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
08049000-0804a000 r--p 00000000 08:04 753598 /home/bobby/Desktop/c/a.out
0804a000-0804b000 rw-p 00001000 08:04 753598 /home/bobby/Desktop/c/a.out
09b79000-09b9a000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77df000-b77e0000 rw-p 00000000 00:00 0
b77ea000-b77eb000 rw-p 00000000 00:00 0
b77ed000-b77f0000 rw-p 00000000 00:00 0
bfed2000-bfef3000 rw-p 00000000 00:00 0 [stack]
Aborted
I want to know what is it.This is first time i got a error of this kind.I want to understand the error and find out what is wrong with my program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将关闭
fp1
两次。请查看 return 之前的最后一行:)如果我正确解释了您的程序,我怀疑您想关闭
fp3
。堆栈跟踪中还有一条线索,错误发生在fclose
内部的某个地方。You are closing
fp1
twice. Look at the last line before return :)I suspect you want to close
fp3
instead, if I've interpreted your program correctly. There's also a clue in the stacktrace, the error happens somewhere insidefclose
.