这本书中的代码出了什么问题? [英] what's wrong with this code from a book?

查看:62
本文介绍了这本书中的代码出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自sedgewick'''算法的c部分1-4''中的这个程序是一个

样本客户端程序......使用符号表来查找不同的

一系列键中的值(随机生成或从标准

输入读取),然后按排序顺序打印出来。

#include < stdio.h>

#include< stdlib.h>

#include" item.h"

#include" st.h"


void main(int argc,char * argv [])

{

int n,maxn = atoi(argv [1]),sw = atoi(argv [2]);

Key v;

Item item;

stInit( (n = 0; n< maxn; n ++){

if(sw)

v = itemRand(); $ max $)


else if(itemScan(& v)== EOF)

break;

if(stSearch(v)!= NULL)

继续;

key(item)= v;

stInsert(item);

}

stSort(itemShow);

printf(" \ n%d ke ys%d distinct keys \ n",n,stCount());

}

它崩溃 - 总线错误 - 当没有输入时命令行。

当给出一些参数时它可靠地给出了不合理的垃圾

结果但是这可能是代码的另一部分中的一个问题而不是

显示。


来自显示的代码,当

没有给出任何参数时,它出错并不奇怪它?我不确定我是否应该更改

代码,因为上面的代码与书中的内容完全相同,但它应该检查argv [b $ b 1]在使用它之前存在不应该吗?如果它不存在
那么应该调用随机部分?或者是问题

别的东西,也许不在上面的代码中?我不知道。

怎么会认为它应该是什么?


谢谢,本。

this programme from sedgewick''s ''algorithms in c parts 1-4'' "is a
sample client program that ... uses a symbol table to find the distinct
values in a sequence of keys (randomly generated or read from standard
input), then prints them out in sorted order."
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
#include "st.h"

void main(int argc, char *argv[])
{
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
Key v;
Item item;
stInit(maxn);
for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
}
it crashes -- Bus Error -- when there''s no input on the command line.
when some arguments are given it reliably gives incorrent, rubbish
results but that''s probably a problem in another part of the code not
shown.

from the code shown, it''s not at all surprising that it goes wrong when
no arguments are given is it? i''m not sure if i should be changing the
code as the above code is exactly how it is in the book, but it should
be checking if argv[1] exists before using it shouldn''t it?, and if it
doesn''t exist then the random part should be called? or is the problem
something else, maybe not in the above code? i don''t know. how does
anyone think it should be?

thanks, ben.

推荐答案



" ben" < x@x.com>在消息新闻中写道:22 ****************** @ x.com ...

< snip>

"ben" <x@x.com> wrote in message news:22******************@x.com...
<snip>
void main(int argc,char * argv [])
{n /,intn,maxn = atoi(argv [1]),sw = atoi(argv [2]);
< snip>

它崩溃 - 总线错误 - 当命令行没有输入时。


这并不会让我感到惊讶。提供的代码不会检查是否实际存在参数
。如果你提到任何引用

argv [1]或argv [2](如上所述)将取消引用argv []

数组的元素存在。因此,结果是未定义的行为。


此外,


无效主要(无论如何)


是不正确的。 Main应返回一个整数。


它很容易通过


< code>

int来补救main(argc,char * argv [])

{

if(argc< 3)/ * argv [0] - >程序,argv [1] - >第一个论点...... * /

{

fprintf(stderr,Insufficient arguments\\\
);

退出(1) ;

}


......等......


返回0;

}


< / code>
从显示的代码
,当
出错时,这并不奇怪没有给出任何论据吗?


不。如果它有效,我会感到很惊讶。


我不确定我是否应该更改代码,因为上面的代码正如书中所示,但它应该
在使用它之前检查是否存在argv [1]不应该吗?


检查argc,它会给你argv []中的项目数。

argv [0](按惯例?POSIX)应该给你启动你的
计划的命令。

谢谢,本。
void main(int argc, char *argv[])
{
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]); <snip>
it crashes -- Bus Error -- when there''s no input on the command line.
That does not surprize me. The code presented does not check wether or not
the arguments are actually present. In the case you mention any reference to
argv[1] or argv[2] (as above) would dereference an element of the argv[]
array that does not exist. Hence undefined behavior is the result.

Furthermore,

void main(whatever)

is incorrect. Main should return an integer.

It''s easily remedied by

<code>
int main(argc, char* argv[])
{
if(argc < 3) /* argv[0] --> program, argv[1]--> first argument ... */
{
fprintf(stderr, "Insufficient arguments\n");
exit(1);
}

... etc ...

return 0;
}

</code>
from the code shown, it''s not at all surprising that it goes wrong when
no arguments are given is it?
Nope. I would be surprized if it worked.

i''m not sure if i should be changing the code as the above code is exactly how it is in the book, but it should
be checking if argv[1] exists before using it shouldn''t it?
Check argc instead, which will give you the number of items in argv[].
argv[0] (by convention?POSIX) should give you the command that started your
program.
thanks, ben.




我很高兴。



My pleasure.


在文章< 41 ********************* @ dreader11.news.xs4all.nl> ,

蒲公英< da ******* @ meadow.net>写道:
In article <41*********************@dreader11.news.xs4all.nl> ,
dandelion <da*******@meadow.net> wrote:
" ben" < x@x.com>在消息新闻中写道:22 ****************** @ x.com ...
....检查argc,它会给你数量argv []中的项目。
argv [0](按惯例?POSIX)应该给你启动你的程序的命令。
"ben" <x@x.com> wrote in message news:22******************@x.com... .... Check argc instead, which will give you the number of items in argv[].
argv[0] (by convention?POSIX) should give you the command that started your
program.
谢谢,本。



我很高兴。



My pleasure.




很棒。我知道所有这一切,但并不完全确定 - 需要

确认 - 非常感谢澄清。


仍然困惑于但它的一部分 - 本书中有关数据的部分

由以下代码使用:...(随机生成或从标准

输入读取)。 .." ;.什么命令行调用会使

以下程序随机生成数据你认为? (实际上我是

在提供数据时甚至不确定命令。

../a.out 4 2 4 6 8

似乎是一个合理的猜测,第一个数字表示有多少数字

跟着它,但我得到了无用的结果,但正如我之前所说的那样

i认为可能是一个其他代码模块中的问题,以便命令

行可能是正确的)


所以,简而言之,你觉得这个程序应该如何

被调用(提供数据,并让它随机生成)?


(支持文件使用的数据类型是整数,但是因为我说b $ b说这些文件可能有问题,但在我之前我会想要确保这部分是应该的那样。

#include< stdio.h>

#include< stdlib.h>

#include" item.h"

#include" st.h"

int main(int argc,char * argv [])

{

if(argc< 3)

fprintf(stderr,Insufficient arguments\\\
),退出(1);

int n,maxn = atoi(argv [1]), sw = atoi(argv [2]);

Key v;

Item item;

stInit(maxn);

for(n = 0; n< maxn; n ++){

if(sw)

v = itemRand();

否则if(itemScan(& v)== EOF)

break;

if(stSearch(v)!= NULL)

继续;

key(item)= v;

stInsert(item);

}

stSort(itemShow) ;

printf(" \ n%d keys%d distinct keys\\\
,n,stCount());

返回0;

}

谢谢,本。



great. i kind of knew all that but wasn''t completely sure -- needed
confirming -- so thanks very much for the clarification.

still confused on part of it though -- this part in the book about data
used by the below code: "... (randomly generated or read from standard
input) ...". what would the command line call look like that makes the
below programme randomly generate the data do you think? (in fact i''m
not even sure about the command when feeding it data.
../a.out 4 2 4 6 8
seems a reasonable guess, the first number saying how many numbers
follow it, but i get useless result from that but as i said previously
i think that maybe a problem in the other code modules so that command
line might be right)

so, all that in short, how do you think this programme is supposed to
be called (to supply data, and make it randomly generate it)?

(the type of data that the support files are using are ints, but as i
say there''s possibly something wrong with those files but before i
tackle those i want to make sure this part is as it should be)
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
#include "st.h"

int main(int argc, char *argv[])
{
if(argc < 3)
fprintf(stderr, "Insufficient arguments\n"), exit(1);
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
Key v;
Item item;
stInit(maxn);
for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
return 0;
}
thanks, ben.




" ben" < x@x.com>在留言新闻中写道:22 ****************** @ x.com ...

"ben" <x@x.com> wrote in message news:22******************@x.com...
文章< 41 ****** ***************@dreader11.news.xs4all.nl> ,
蒲公英< da ******* @ meadow.net>写道:
In article <41*********************@dreader11.news.xs4all.nl> ,
dandelion <da*******@meadow.net> wrote:
" ben" < x@x.com>在消息新闻中写道:22 ****************** @ x.com ......
"ben" <x@x.com> wrote in message news:22******************@x.com... ...
检查argc,它会给你argv []中的项目数。
argv [0](按惯例?POSIX)应该为你提供启动
你的程序的命令。
Check argc instead, which will give you the number of items in argv[].
argv[0] (by convention?POSIX) should give you the command that started your program.
thanks ,本。
thanks, ben.



我的荣幸。



My pleasure.



很棒。我知道所有这一切,但并不完全确定 - 需要
确认 - 所以非常感谢澄清。

仍然困惑于它的一部分 - 这部分在书中关于以下代码使用的数据:......(随机生成或从标准
输入读取)......。命令行调用会是什么使得
以下程序随机生成数据你认为? (实际上我在给它提供数据时甚至不确定这个命令。
./a.out 4 2 4 6 8
似乎是一个合理的猜测,第一个数字怎么说许多数字跟着它,但是我得到了无用的结果,但正如我先前所说的那样,我认为在其他代码模块中可能存在问题,因此命令
行可能是正确的)
(支持文件正在使用的数据类型是整数,但正如我说这些文件可能有问题但在我解决之前我想确保这部分就像它一样应该是)

#include< stdio.h>
#include< stdlib.h>
#include" item.h"
#include" ; st.h"

int main(int argc,char * argv [])
{
if(argc< 3)



great. i kind of knew all that but wasn''t completely sure -- needed
confirming -- so thanks very much for the clarification.

still confused on part of it though -- this part in the book about data
used by the below code: "... (randomly generated or read from standard
input) ...". what would the command line call look like that makes the
below programme randomly generate the data do you think? (in fact i''m
not even sure about the command when feeding it data.
./a.out 4 2 4 6 8
seems a reasonable guess, the first number saying how many numbers
follow it, but i get useless result from that but as i said previously
i think that maybe a problem in the other code modules so that command
line might be right)

so, all that in short, how do you think this programme is supposed to
be called (to supply data, and make it randomly generate it)?

(the type of data that the support files are using are ints, but as i
say there''s possibly something wrong with those files but before i
tackle those i want to make sure this part is as it should be)
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
#include "st.h"

int main(int argc, char *argv[])
{
if(argc < 3)



{fprintf(stderr,&quo t;参数不足\ n;);出口(1);
}


花括号* do *属于那里,分号'';''标志着

a语句的结束,而不是逗号。

int n,maxn = atoi(argv [1]),sw = atoi(argv [2]);
键v;
项目项;


声明在*第一个声明之前*。

stInit(maxn);
for(n = 0; n< maxn; n ++){
if(sw)
v = itemRand();
else if(itemScan(& v)= = EOF)
break;
if(stSearch(v)!= NULL)
继续;
key(item)= v;
stInsert(item);
}
stSort(itemShow);
printf(" \ n%d keys%d distinct keys\\\
,n,stCount());
返回0;
}


{ fprintf(stderr, "Insufficient arguments\n"); exit(1); }

The curly-braces *do* belong there and a semicolon '';'' marks the end of
a statement, not a comma.
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
Key v;
Item item;
The declarations go *before* the first statement.
stInit(maxn); for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
return 0;
}




这样就会变成


int main(int argc,char * argv [])< br $>
{

int n,maxn;

Key v;

Item item;

if(argc< 3)

{

/ *没有给出命令行args * /

fprintf(stderr, 参数不足,使用随机值\ n;);

maxn = random()* WHATEVER_MAXN / MAX_RAND;

sw = random()* WHATEVER_SW / MAX_RAND;

}

其他

{

/ *命令行参数* /

maxn = atoi(argv [1]);

sw = atoi(argv [2]);

}


/ * at这一点maxn和sw ae定义为* /

for(n = 0; n< MAXN; n ++){

if(sw)

v = itemRand();

else if(itemScan(& v)== EOF)

休息;

如果(stSearch(v)!= NULL)

继续;

key(item)= v;

stInsert(item);

}

stSort(itemShow);

printf(" \\ \\ n%d键%d个不同的键\ n",n,stCount());

返回0;

}


如果提供了非数字参数,上面的*仍会*中断,顺便说一下。如果

你想要处理它,请使用sscanf而不是atoi并检查

结果。



So that would become

int main(int argc, char* argv[])
{
int n, maxn;
Key v;
Item item;

if(argc < 3)
{
/* no command line args given */
fprintf(stderr, "Insufficient arguments, using random values\n");
maxn = random() * WHATEVER_MAXN/MAX_RAND;
sw = random() * WHATEVER_SW/MAX_RAND;
}
else
{
/* command line args given */
maxn = atoi(argv[1]);
sw = atoi(argv[2]);
}

/* at this point maxn and sw ae defined either way */
for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
return 0;
}

The above will *still* break if non-numeric arguments are provided, btw. If
you want to handle that aswell, use sscanf instead of atoi and check the
results.


这篇关于这本书中的代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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