计划结果不一致 [英] Inconsistent Program Results

查看:80
本文介绍了计划结果不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C,享受弦乐和乐趣;目前指针!


以下程序是我对练习进行输入的解决方案,

剥离第一个单词,然后输出其余单词。当你给它2个或更多单词时,它可以正常工作,但当只有1个单词时,结果会有所不同

,具体取决于它是在Windows上还是在Linux:在MSVC下它显示

没有输出(应该如此);在gcc / Linux下它反而给出了

分段错误。


任何想法发生了什么?


TIA!

#include< malloc.h>

#include< stdio.h>

#include< memory.h>


#define LEN 1000


void rdinpt();


无效main()

{

char * s,* restrict;

rdinpt(& s);

restrict = strchr(s,'''');

printf("%s\ n",++ restrict);

free(s),s = restrict = 0;

返回(0);

}


void rdinpt(char ** s)

{

* s =(char *)malloc((unsigned int)LEN);

(void)得到(* s);

返回(0);

}

I am learning C, having fun with strings & pointers at the moment!

The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there''s only 1 word the results vary
depending on whether it''s on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".

Any ideas what''s going on?

TIA!
#include <malloc.h>
#include <stdio.h>
#include <memory.h>

#define LEN 1000

void rdinpt();

void main()
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,'' '');
printf("%s\n", ++restrict);
free(s), s=restrict=0;
return (0);
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);
(void) gets(*s);
return(0);
}

推荐答案

Fr ************ @ googlemail.com 说:

我正在学习C,和st一起玩戒指&目前指针!


以下程序是我对练习进行输入的解决方案,

剥离第一个单词,然后输出其余单词。当你给它2个或更多单词时,它可以正常工作,但当只有1个单词时,结果会有所不同

,具体取决于它是在Windows上还是在Linux:在MSVC下它显示

没有输出(应该如此);在gcc / Linux下它反而给出了

分段错误。


任何想法发生了什么?


TIA!


#include< malloc.h>
I am learning C, having fun with strings & pointers at the moment!

The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there''s only 1 word the results vary
depending on whether it''s on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".

Any ideas what''s going on?

TIA!
#include <malloc.h>



将此替换为< stdlib.h,这是使用malloc

时所需的标准头。

Replace this with <stdlib.hwhich is the standard header you need when
using malloc.


#include< stdio.h>
#include <stdio.h>



这很好。

This is fine.


#include< memory.h>
#include <memory.h>



将此替换为< string.hwhich是使用strchr

时所需的标准头。

Replace this with <string.hwhich is the standard header you need when
using strchr.


#define LEN 1000

void rdinpt();
#define LEN 1000

void rdinpt();



制作:


void rdinput(char **);


顺便提一下,readinput是一个更好的名字。

Make this:

void rdinput(char **);

Incidentally, readinput would have been a better name.


>

void main()
>
void main()



制作:


int main(无效)

Make this:

int main(void)


{

char * s,* restrict;

rdinpt(& s);

restrict = strchr(s,'''');
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,'' '');



如果malloc失败,s将具有NULL值,这对于

传递给strchr是不合法的。


如果输入字符串中没有空格,strchr将返回NULL,

你不能传递给printf来匹配%s,你不能

增量。

If the malloc fails, s will have the value NULL, which is not legal for
passing to strchr.

If there is no space in the input string, strchr will return NULL, which
you must not pass to printf to match %s, and which you must not
increment.


printf("%s \ n",++ restrict);

免费(s) ),s = restrict = 0;
printf("%s\n", ++restrict);
free(s), s=restrict=0;



这让我想到了这本书。简化:


free(s);

s = restrict = 0;

This made me reach for the book. Simplify:

free(s);
s = restrict = 0;


return( 0);
return (0);



返回0;


没问题。 return关键字不是函数名。

return 0;

is fine. The return keyword is not a function name.


}


void rdinpt(char ** s)

{

* s =(char *)malloc((unsigned int)LEN);
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);



更好:


* s = malloc(LEN * sizeof ** s);

一般形式是:p = malloc(n * sizeof * p);

Better:

*s = malloc(LEN * sizeof **s);

The general form is: p = malloc(n * sizeof *p);


(void)gets(* s);
(void) gets(*s);



避免gets() - 它不能安全使用。相反,使用:


fgets(s,LEN,stdin);

Avoid gets() - it cannot be used safely. Instead, use:

fgets(s, LEN, stdin);


return(0);
return(0);



删除它,因为rdinpt没有返回值。

Remove this, since rdinpt doesn''t return a value.


}
}



-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Fr ************ @ googlemail.com 写道:

我正在学习C,享受弦乐和乐趣;指尖此刻!
I am learning C, having fun with strings & pointers at the moment!


以下程序是我对练习进行输入的解决方案,

剥离第一个单词,然后输出休息。当你给它2个或更多单词时,它可以正常工作,但当只有1个单词时,结果会有所不同

,具体取决于它是在Windows上还是在Linux:在MSVC下它显示

没有输出(应该如此);在gcc / Linux下它反而给出了

分段错误。
The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there''s only 1 word the results vary
depending on whether it''s on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".


#include< malloc.h>
#include <malloc.h>



这不是标准标题。你可能应该包括

< stdlib.hinstead。

This is not a standard header. You probably should be include
<stdlib.hinstead.


#include< stdio.h>

#include< memory.h>
#include <stdio.h>
#include <memory.h>



另一个非标准的标题,我不知道你需要什么?b $ b需要它。避免使用非标准标题,除非你知道

为什么要使用它们,即如果你做了一些系统特定的事情

- 但你的程序中没有任何系统特定的

需要这个。

Another non-standard header, I have no idea what you would
need that for. Avoid non-standard headers unless you know
why you use them, i.e. if you do something system-specific
- but there''s nothing system-specific in your program that
would require this.


#define LEN 1000
#define LEN 1000


void rdinpt();
void rdinpt();



为什么骗骗编译器?你定义的同名函数

后面带一个参数,一个指向指针的指针

char。

Why do you lie to the compiler? The function of the same name
you define later takes an argument, a pointer to pointer to
char.


void main()
void main()



在Windows和Linux下,main()总是返回一个int(并且

你的主函数实际返回一个INT)。并且要明白它不会更好地把它写成


int main(void):

Under both Windows and Linux main() always returns an int (and
your main function aactually returns an int). And to make it
clear that it doesn''t take any arguments better write it as

int main( void ):


{

char * s,* restrict;

rdinpt(& s);

restrict = strchr(s,'''');
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,'' '');



如果字符串中没有空格,则strchr()返回

NULL。

If there''s no space in the string you got then strchr() returns
NULL.


printf("%s \ n",++ restrict);
printf("%s\n", ++restrict);





字符串中没有空格的情况下强制printf()你必须取消引用NULL + 1 - 无论如何可能会指出

很可能这不是你拥有的任何记忆。并且

在这种情况下你有不确定的行为,一切都可以发生。没有打印或segzeulting只是可能性中的两个。


所以你需要检查strchr()的返回值并且只需要

尝试打印出来的东西,如果那不是空的话。

That forces printf() in the case that there was no space in the
string you got to dereference NULL+1 - whatever that may point
to it''s rather likely that this isn''t any memory you own. And
in that case you have undefined behaviour and everything can
happen. Printing nothing or segfaulting are only two of many
possibilities.

So you need to check the return value of strchr() and only
try to print out something if that wasn''t NULL.


free(s),s = restrict = 0;
free(s), s=restrict=0;



为什么在这里使用逗号运算符而不是分号?并且

因为''s'和''restrict''都是指针,你最好通过指定NULL来清除
。下一个有
读取代码的人会感谢你。

Why use the comma operator instead of a semicolon here? And
since both ''s'' and ''restrict'' are pointers you better make
that clear by assigning NULL instead. The next person having
to read your code will thank you.


return(0);

}
return (0);
}


void rdinpt(char ** s)

{

* s =(char *)malloc((unsigned int)LEN);
void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);



为什么演员阵容为''char *''?我怀疑你把它放在那里以保持编译器抱怨从一个

int到指针的赋值。但是铸造并不是解决方案的原因,只会让事情变得更糟。你需要包含

< stdlib.h,以使编译器知道malloc()返回

a void指针而不是int,因为它会假设它是

不知道malloc()的返回类型。顺便说一句,

malloc()的参数类型为''size_t'',而不是unsigned

int。但在这里施法也是多余的。如果编译器

知道什么样的参数malloc()期望它将在必要时自行完成

转换(另一个参数为

包括< stdlib.h>)。


如果你使用malloc()永远不会忘记检查它的返回值。

它可能会失败,返回NULL ,然后你不仅没有

内存,但是如果你使用这个返回值你的程序可能会崩溃

或做更糟糕的事情。

Why the cast to ''char*''? I suspect you put it there to keep
the compiler from complaining about an assignment from an
int to a pointer. But casting is not the solution in that
case, it only makes things worse. You need to include
<stdlib.hto make the compiler aware that malloc() returns
a void pointer and not an int, as it will assume if it does
not know about the return type of malloc(). And, by the way,
the argument to malloc() is of type ''size_t'', not unsigned
int. But casting here is also superfluous. If the compiler
knows what kind of argument malloc() expects it will do the
conversion all by itself if necessary (another argument for
including <stdlib.h>).

And if you use malloc() never forget to check its return value.
It can fail, returning NULL, and then you not only have no
memory but if you use this return value your program may crash
or do even worse things.


(void)得到(* s);
(void) gets(*s);



现在你使用了你以前从未使用的_the_函数。

它被设计破坏了,因为如果用户输入超过

LEN-1字符函数将在你分配的内存结束后写入

。所以,从来没有想过使用它的

。使用fgets()代替 - 你可以告诉你如何准备好阅读许多字符。

And now you used _the_ function you never ever should use.
It is broken by design since if the user enters more than
LEN-1 characters the function will write past the end of
the memory you allocated. So, never ever even think of
using it. Use fgets() instead - there you can tell how
many chars you are prepared to read in.


return(0);
return(0);



奇怪,上面你说这个功能没有回来

任何......而且,顺便说一句,你不要不需要括号

返回值,''return''不是函数。

Strange, above you told that the function doesn''t return
anyting... And, by the way, you don''t need parentheses
around the return value, ''return'' isn''t a function.


}
}



如果您还没有要求编译器输出批次

的警告(例如至少使用选项''-W -Wall''如果

你使用gcc)。试着理解是什么让编译器抱怨并纠正你的程序,以便它干净地编译

。这将教会你很多关于你b $ b的错误。

问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


文章< 11 ***** *****************@q40g2000cwq.googlegroups .com> ;,
Fr ************ @ googlemail.com 写道:
In article <11**********************@q40g2000cwq.googlegroups .com>,
Fr************@googlemail.com wrote:

我正在学习C,有有趣的字符串&目前指针!


以下程序是我对练习进行输入的解决方案,

剥离第一个单词,然后输出其余单词。当你给它2个或更多单词时,它可以正常工作,但当只有1个单词时,结果会有所不同

,具体取决于它是在Windows上还是在Linux:在MSVC下它显示

没有输出(应该如此);在gcc / Linux下它反而给出了

分段错误。


任何想法发生了什么?
I am learning C, having fun with strings & pointers at the moment!

The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there''s only 1 word the results vary
depending on whether it''s on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".

Any ideas what''s going on?



< delurk>

我会对此采取行动,其他人可能会发现其他问题。

<delurk>
I''ll take a crack at this, others might find other problems, though.


>

TIA!


#include< malloc.h>
>
TIA!
#include <malloc.h>



这是一个非标准的标题; malloc是在stdlib.h中声明的,所以你要
应该包含那个标题。

This is a nonstandard header; malloc is declared in stdlib.h, so you
should include that header.


#include< stdio.h>

#include< memory.h>
#include <stdio.h>
#include <memory.h>



另一个看似无所作为的非标准标题。

Another nonstandard header that doesn''t seem to do anything.


>

#define LEN 1000


void rdinpt();
>
#define LEN 1000

void rdinpt();



这应该是

void rdinpt(char **);

This should be
void rdinpt (char **);


>

void main()
>
void main()



返回类型main是int,所以这应该是int main(),

或int main(void),大多数常客我​​认为会推荐第二个。

The return type of main is int, so this should be int main (),
or int main (void), most regulars I believe would recommend the second.


{

char * s,* restrict;

rdinpt(& s);

restrict = strchr(s,'''');

printf("%s \ n",++ restrict);
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,'' '');
printf("%s\n", ++restrict);



这里有一个问题,检查标准(或手册页),如果strchr

找不到角色正在搜索,它将返回值

设置为NULL,就像您只输入一个单词时的情况一样。然后你尝试使用
增加一个NULL指针,这是'未定义的行为,所以任何东西都可以发生,包括什么都不做或者是segfaulting。你必须通过确保限制不是NULL来防范

,所以做一些事情

喜欢:

if(restrict)

{

printf("%s\ n",++ restrict);

}

由方式,restrict是C中的类型限定符,因此它可能不是变量的好名称。

Here''s one problem, check the standard (or the man page), if strchr
doesn''t find the character being searched for, it sets the return value
to NULL, as will be the case when you input only one word. You then try
to increment a NULL pointer, that''s undefined behavior, so anything can
happen, including doing nothing or segfaulting. You have to guard
against this by making sure that restrict is not NULL, so do something
like:
if (restrict)
{
printf ("%s\n", ++restrict);
}
By the way, restrict is a type qualifier in C, so it''s probably not a
good name for a variable.


free(s),s = restrict = 0;
free(s), s=restrict=0;



如果要将这些指针设置为NULL,请说明。虽然,既然你是免费的,那么结束这个项目,就没有必要了。

If you want to set these pointers to NULL, say so. Although, since you
free s then end the program, it''s not necessary.


return(0);
return (0);



0左右的括号是不必要的(但没有错)

The parentheses around 0 are unnecessary (but not wrong)


}


void rdinpt(char ** s)

{

* s =(char *)malloc((unsigned int)LEN);
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);



在C中,转换为char *是不必要的,可以掩盖#include

< stdlib.h>的失败。此外,LEN将自动转换为size_t(我相信),因此无需转换为unsigned int。另外,在使用s之前使用
之前,你需要确保malloc成功分配了所请求的

内存,如果它没有,则s将为NULL,所以测试s对于NULL,比如

我为限制做了以上。

In C, the cast to char * is unnecessary and can mask failure to #include
<stdlib.h>. Also, LEN will be automatically converted to size_t (I
believe), so the cast to unsigned int is unnecessary. Also, before
using s, you need to make sure that malloc sucessfully allocated the
memory requested, if it didn''t, s will be NULL, so test s for NULL, like
I did for restrict above.


(void)得到(* s);
(void) gets(*s);



绝不使用获取;你不能阻止缓冲区溢出。最好使用

fgets,或者使用CBFalconer'的ggets(我很确定他很快就可以使用下载页面的

URL)

Never use gets; you cannot prevent buffer overflow. Better to use
fgets, or use CBFalconer''s ggets (I''m sure he''ll be along soon with the
URL for his download page)


return(0);
return(0);



你宣布这个函数的返回类型为void,即没有返回,

所以不要返回任何东西。

You declared this function with a return type of void, i.e. no return,
so don''t return anything.


}
}



这就是我能在这里找到的(我怎么办? )

< \ delurk>

That''s all I can find here (how''d I do?)
<\delurk>


这篇关于计划结果不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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