C语言读写txt文件的问题

C_arm 2012-06-29 10:03:41
写了一个用C语言读写文件的程序,发现关闭文件的时候报了一个堆栈溢出的错误,系统为windowsXP,编译器是VS2008,代码如下:

FILE* inputXrayPoints1;
int numPoints1;
double* xrayVideoPointsCorrespondence1;
int iterPoints;

double xrayPointX;
double xrayPointY;
double videoPointX;
double videoPointY;
int point3DX;
int point3DY;

inputXrayPoints1=fopen("testCase/pointsCorrespondence1.txt","r");
fscanf(inputXrayPoints1,"%d",&numPoints1);
//fprintf(stdout,"numPoints1:\t%d\n",numPoints1);
xrayVideoPointsCorrespondence1=(double*)malloc(6*numPoints1);
for(iterPoints=0;iterPoints<numPoints1;++iterPoints){
fscanf(inputXrayPoints1,"%lf%lf%lf%lf%d%d",&xrayPointX,&xrayPointY,&videoPointX,&videoPointY,&point3DX,&point3DY);
xrayVideoPointsCorrespondence1[6*iterPoints]=xrayPointX;
xrayVideoPointsCorrespondence1[6*iterPoints+1]=xrayPointY;
xrayVideoPointsCorrespondence1[6*iterPoints+2]=videoPointX;
xrayVideoPointsCorrespondence1[6*iterPoints+3]=videoPointY;
xrayVideoPointsCorrespondence1[6*iterPoints+4]=(double)point3DX;
xrayVideoPointsCorrespondence1[6*iterPoints+5]=(double)point3DY;
fprintf(stdout,"Point:\t%d\n%lf\t%lf\t%lf\t%lf\t%d\t%d\n",iterPoints,xrayPointX,xrayPointY,videoPointX,videoPointY,point3DX,point3DY);
}
fclose(inputXrayPoints1);
...全文
198 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
C_arm 2012-06-29
  • 打赏
  • 举报
回复
问题解决
xrayVideoPointsCorrespondence1=(double*)malloc(6*numPoints1);

应改为

xrayVideoPointsCorrespondence1=(double*)malloc(6*numPoints1*sizeof(double));

多谢楼里的两位,分数各给一半
C_arm 2012-06-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

加断点,单步调试。指针再使用之前必须有指向的内存区域。使用完成后记得释放
[/Quote]

为什么要释放?
要是我下边还要用呢?
C_arm 2012-06-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

xrayVideoPointsCorrespondence1=(double*)malloc(6*numPoints1);

请问下这句的释放在哪里?

还有 numPoints有初始化么?
[/Quote]

初始化了
应改为
int numPoints1=0;

为什么要释放?那块内存我之后还要用
xrayVideoPointsCorrespondence1在底下的程序里还有用
W170532934 2012-06-29
  • 打赏
  • 举报
回复
加断点,单步调试。指针再使用之前必须有指向的内存区域。使用完成后记得释放
小新小小新 2012-06-29
  • 打赏
  • 举报
回复
xrayVideoPointsCorrespondence1=(double*)malloc(6*numPoints1);

请问下这句的释放在哪里?

还有 numPoints有初始化么?

70,026

社区成员

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

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