奇怪的行为 [英] strange behavior

查看:106
本文介绍了奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了一件非常奇怪的事。我不知道这是一个问题还是一个实施问题。该程序从

读取一个文件中的数据并将其加载到两个数组中。当xy,x,y,* xlist和

* ylist是整数或浮动时,没有明显的问题。如果这些

变量是双精度数,则程序崩溃。此外,仅当xlist和ylist为free()时才会发生

崩溃。当

以上的变量是双倍的并且对free()的调用被取消时,程序并没有崩溃,但输出很奇怪。


如果有人有足够的时间在他手上

编译这段代码并玩它,我会很感激

你的意见。不要忘记在更改类型时更改

调用fscanf()中的格式说明符。以下是

输入文件的数据:


0 12 5 12 8 9 11 3 10 -2 5 -8 0 -10 -7 -1 - 7 12

/ *从文件读取xy数据。

将x和y值加载到相应的数组中。

使用整数,浮点数,但不是双打???

* /

#include< stdio.h>

#include< stdlib。 h>


int main(无效)

{

双xy,x,y;

double * xlist,* ylist;

int xct,yct,xycount,idx;

FILE * data;


if ((data = fopen(" vertices.txt"," r"))== NULL)

{

fprintf(stderr," can''t打开数据文件\ n");

退出(EXIT_FAILURE);

}

xycount = 0;

while(fscanf(data,"%lf")== 1)

xycount ++;

if(xycount%2!= 0)

{

fprintf(stderr,不等数x,y值\ n);

退出(EXIT_FAILURE);

}

xlist = calloc(xycount / 2,sizeof(xlist));

ylist = calloc(xycount / 2,sizeof(ylist));

倒带(数据);

xct = 0;

yct = 0;

while(fscanf(data," %lf%lf",& x,& y)== 2)

{

xlist [xct ++] = x;

ylist [yct ++] = y;

}

fclose(数据);


for(idx = 0; idx< xycount / 2; ++ idx)

printf("%4.f",xlist [idx]);

putchar(''\ n'');

for(idx = 0; idx< xycount / 2; ++ idx)

printf("%4.f",ylist [idx]);

putchar(''\ n'');


免费(xlist);

免费(ylist);


返回0;

}

Something very strange is going on here. I don''t know if it''s a C
problem or an implementation problem. The program reads data from
a file and loads it into two arrays. When xy, x, y, *xlist and
*ylist are ints or floats there is no apparent problem. If these
variables are doubles, the program crashes. Furthermore, the
crashes occur only when xlist and ylist are free()ed. When the
above variables are doubles and the calls to free() are
eliminated, the program doesn''t crash, but the output is weird.

If there is anyone out there with enough time on his hands to
compile this code and play with it, I would appreciate getting
your opinion. Don''t forget to change the format specifiers in the
calls to fscanf() when changing types. Here are data for the
input file:

0 12 5 12 8 9 11 3 10 -2 5 -8 0 -10 -7 -1 -7 12

/* Read xy data from a file.
Load x&y values into respective arrays.
Works with ints, floats, but not doubles???
*/
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double xy, x, y;
double *xlist, *ylist;
int xct, yct, xycount, idx;
FILE *data;

if((data = fopen("vertices.txt", "r")) == NULL)
{
fprintf(stderr, "can''t open data file\n");
exit(EXIT_FAILURE);
}
xycount = 0;
while(fscanf(data, "%lf") == 1)
xycount++;
if(xycount%2 != 0)
{
fprintf(stderr, "unequal number of x,y values\n");
exit(EXIT_FAILURE);
}
xlist = calloc(xycount/2, sizeof(xlist));
ylist = calloc(xycount/2, sizeof(ylist));
rewind(data);
xct = 0;
yct = 0;
while(fscanf(data, "%lf %lf", &x, &y) == 2)
{
xlist[xct++] = x;
ylist[yct++] = y;
}
fclose(data);

for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", xlist[idx]);
putchar(''\n'');
for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", ylist[idx]);
putchar(''\n'');

free(xlist);
free(ylist);

return 0;
}

推荐答案

Marlene Stebbins< st * *****@email.com>这样说:
Marlene Stebbins <st******@email.com> spoke thus:
while(fscanf(data,"%lf")== 1)
while(fscanf(data, "%lf") == 1)



^

您正在阅读的浮标在哪里?如果这是你的实际代码,

难怪它会崩溃。如果你想忽略一些输入,请使用


fscanf(data,"%* lf"); / * legal * /


我不知道这是否是你描述的行为的根源,

但这肯定是个问题。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


^
Where is the float you''re reading going? If this is your actual code,
no wonder it crashes. If you want to ignore some input, use

fscanf( data, "%*lf" ); /* legal */

I have no idea whether that''s the root of the behavior you describe,
but it''s definitely a problem.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


在文章< 40 ************** @ email.com> ;, Marlene Stebbins写道:
In article <40**************@email.com>, Marlene Stebbins wrote:
这里有一些非常奇怪的事情。>

xlist = calloc(xycount / 2,sizeof(xlist));
ylist = calloc(xycount / 2,sizeof (ylist));
Something very strange is going on here.>

xlist = calloc(xycount/2, sizeof(xlist));
ylist = calloc(xycount/2, sizeof(ylist));




你想要


xlist = calloc(xycount / 2,sizeof * xlist);

ylist = calloc(xycount / 2,sizeof * ylist);


其他类型的列表工作的原因是,e />
例如,sizeof xlist和sizeof * xlist。


-

Neil Cerutti

你想看到吗? 'em?"

"看什么?"

"尸体。他们在地下室。 --_活死人归来。



You want

xlist = calloc(xycount/2, sizeof *xlist);
ylist = calloc(xycount/2, sizeof *ylist);

The reason that other types of lists work is a coincedence of,
e.g., sizeof xlist and sizeof *xlist.

--
Neil Cerutti
"Do you wanna see ''em?"
"See what?"
"The corpses. They''re in the basement." --_Return of the Living Dead_.


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
Marlene Stebbins< st ****** @ email .COM>这样说:

Marlene Stebbins <st******@email.com> spoke thus:

while(fscanf(data,"%lf")== 1)
while(fscanf(data, "%lf") == 1)



^
fscanf(data,"%* lf"); / * legal * /

我不知道这是你描述的行为的根源,
但这肯定是个问题。



^
Where is the float you''re reading going? If this is your actual code,
no wonder it crashes. If you want to ignore some input, use

fscanf( data, "%*lf" ); /* legal */

I have no idea whether that''s the root of the behavior you describe,
but it''s definitely a problem.



好​​的,谢谢。虽然不会影响这种奇怪的行为。


Marlene



OK, thanks. Doesn''t affect the strange behavior though.

Marlene


这篇关于奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆