请帮我克服分段错误 [英] Please help me overcome the segmentation fault

查看:65
本文介绍了请帮我克服分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在Red Hat 9上使用GCC。我遇到了一些我认为不应该发生的问题
(至少在Turbo C上),

这里他们r:


为什么以下代码显示分段错误错误:


{

int list [] = {10,20,30,40,50};

int * p,* q;

p = list; //问题出在这里,我用

调试器检查

q = list + 4;

}

另一个问题是,在使用fgets的时候我必然会使用getchar来支持
删除stdin中未读的''\ n''character,因为fflush(stdin)不会工作。


char temp [20];

printf(" \ n输入字符串:");

的getchar(); // fflush(stdin)本来应该用的是

fgets(temp,20,stdin);


这样工作正常但我认为不是一个正确的方法。 PLZ

建议一个解决方案。为什么不是fflush(stdin)工作,有没有替换fflush(stdin)的




plz帮助...在此先感谢

解决方案

代码看起来很好。之后会发生什么?




Rav写道:


char temp [20];

printf(" \ n输入字符串:");

getchar(); // fflush(stdin)本来应该用的是

fgets(temp,20,stdin);


这样工作正常但我认为不是一个正确的方法。 PLZ

建议一个解决方案。为什么不是fflush(stdin)工作,是否有任何替换fflush(stdin)的




plz help ...在此先感谢



在终端上使用fflush()不是一个好主意。


如果有一个额外的\ n ;,很可能你有一个先前的scanf()

没有吸收尾随的\ n。要么使用getchar(),要么使用

先前读取或scanf()也读取\ n。


< blockquote> Rav写道:


我最近开始在Red Hat 9上使用GCC。我遇到了一些我觉得有些问题的b $ b不应该发生(至少在Turbo C上),

这里他们r:


为什么以下代码显示分段错误错误:


{

int list [] = {10,20,30,40,50};

int * p,* q;

p = list; //问题出在这里,我用

调试器检查

q = list + 4;

}


我看不出这个碎片本来就没有错,

所以我怀疑问题可能在我看不到的代码中。

请发布一个最小的,完整的,可编译的例子,

演示了你的问题。


另一个问题是,在使用fgets时我必然会使用getchar to

删除stdin中未读的''\ n'字符,因为fflush(stdin)不起作用。



通常,fgets()读取并消耗输入流

,直到它读取并消耗了一个''\ n''' ,终止该行的那个

。 (例外:fgets()将提前停止

,如果遇到文件结尾

或输入错误,则不会读取''\ n''如果缓冲区的行太长了。)

我不确定你的意思是未读'\ n''字符,

因为通常不会是一个。


char temp [20];

printf(" \\\
Enter string:");

getchar(); // fflush(stdin)原本应该用



fflush(stdin)是* never *应该被使用。什么

你想完成什么?


fgets(temp,20,stdin);


它的工作方式很好,但我认为这不是一个正确的方法。 PLZ

建议一个解决方案。



你说它工作正常,并且你要求解决方案,但是你

没告诉我们你要解决什么问题。


为什么不是fflush(stdin)工作,有没有替换fflush(stdin)的替代品。



fflush()执行输出操作。 stdin不是输出

流。这就是fflush(stdin)没有工作的原因。就像


fputs(" \ n!dlrow,olleH",stdin);


并没有工作。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}

another problem is that, while using fgets i am bound to use getchar to
remove the unread ''\n''character in stdin as fflush(stdin) doesn''t work.

char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution. why isn''t fflush(stdin) working, is there any
alternative of fflush(stdin).

plz help...thanks in advance

解决方案

The code looks fine as far as it goes. What comes after?



Rav wrote:

char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution. why isn''t fflush(stdin) working, is there any
alternative of fflush(stdin).

plz help...thanks in advance

using fflush() on a terminal is not a great idea.

If there is an extra "\n", it''s likely you had a previous scanf() which
did not absorb the trailing "\n". Either use the getchar() or have the
previous read or scanf() also read the "\n".


Rav wrote:

I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}

I can see nothing inherently wrong with this fragment,
so I suspect the problem may be in the code I cannot see.
Please post a minimal, complete, compilable example that
demonstrates your problem.

another problem is that, while using fgets i am bound to use getchar to
remove the unread ''\n''character in stdin as fflush(stdin) doesn''t work.

Normally, fgets() reads and consumes the input stream
until it has read and consumed exactly one ''\n'', the one
that terminates the line. (Exceptions: fgets() will stop
early, without reading a ''\n'', if it encounters end-of-file
or an input error or if the line is too long for the buffer.)
I''m not sure what you mean by "the unread ''\n'' character,"
since there usually won''t be one.

char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used

fflush(stdin) was *never* supposed to be used. What
are you trying to accomplish?

fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution.

You say "it works fine," and you ask for a "solution," but you
haven''t told us what problem you are trying to solve.

why isn''t fflush(stdin) working, is there any
alternative of fflush(stdin).

fflush() performs an output operation. stdin is not an output
stream. That''s why fflush(stdin) doesn''t "work," just as

fputs("\n!dlrow ,olleH", stdin);

doesn''t "work."

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于请帮我克服分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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