【发布时间】:2014-06-19 17:39:07
【问题描述】:
我不断收到此错误 glibc 双释放或损坏(出)错误 到目前为止,我读到的所有内容都是由于 malloc 使用不当,这将指向内存空间,但是,我没有使用 malloc。我认为它与 fscanf 如何接受流有关。但是我在 fscanf 上阅读的任何内容对我遇到的错误都没有任何意义。这是代码。
#include <stdio.h>
#include <fcntl.h>
#include <sys.stat.h>
#inlcude <termios.h>
#inlcude <string.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS0"
main()
{
int n;
FILE *file
file = fopen(MODEMDEVICE, "a");
if(file == NULL){
printf("initiating error\n);
return 1;
}
FILE *fp;
fp = fopen("testfile.txt", "a");
while(1){
fscanf(file, "%02x", &n);
fprintf(fp, "%d", n);
fclose(fp);
fclose(file);
}
}
我编写此代码的目的是从串行端口获取数据流并将它们存储在文本文件中。关于我为什么会收到此错误的任何想法?
【问题讨论】:
-
“我没有使用 malloc”——是的,你 是(间接地)——
fopen使用它(而fclose使用free),所以滥用fopen可能(并且确实)表现为malloc失败。