Segfault问题。 [英] Segfault question.

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

问题描述

当我开始测试我的包装程序的算法时,我把这个代码片段汇总在一起

,这非常有用。除了它(可预测地)

当它试图超越文件时最终会出现段错误。在某些时候,我使用feof()尝试修复这种行为,但没有成功。

功能没有受到伤害,但这已经开始让我感到烦恼。我在这里缺少什么?
?有时候代码变得令人沮丧!!哈哈!!!


代码:


#include< stdio.h>

#include< ; stdlib.h>

#include< string.h>


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

{

FILE * fp;


int len;

char buf [100];


if((fp = fopen(argv [1]," r"))== NULL){

fprintf(stderr,不能打开fp ;);

返回EXIT_FAILURE;

}


while(((len = strlen(fgets(buf,80,fp) )))!= 0)){

printf("%i \t",len);

printf("%s",buf);

}


fclose(fp); / *不,这里没有错误检查... * /


返回EXIT_SUCCESS;

}


谢谢阅读。

-

电子邮件是wtlyman at olypen dot com

When I started testing the algorithms for my wrap program, I threw together
this snippet of code, which works quite well. Except that it (predictably)
segfaults at the end when it tries to go beyond the file. At some point, I
tried to mend that behavior using feof() but without success. The
functionality is not harmed, but this has started to bug me. What am I
missing here? Sometimes being a code duffer is frustrating!! lol!!!

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
FILE *fp;

int len;
char buf[100];

if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "can''t open fp");
return EXIT_FAILURE;
}

while (((len = strlen(fgets(buf, 80, fp))) != 0)) {
printf(" %i\t", len);
printf("%s", buf);
}

fclose(fp); /* Nah, no error checking here... */

return EXIT_SUCCESS;
}

Thanks for reading.
--
Email is wtallman at olypen dot com

推荐答案



" name" <我们** @ host.domain>在消息中写道

新闻:10 ************* @ corp.supernews.com ...

"name" <us**@host.domain> wrote in message
news:10*************@corp.supernews.com...
当我开始测试算法时对于我的包装程序,我把
扔到了这段代码中,效果很好。


不,不是。它会调用未定义的行为。

除了它(可预测)
在尝试超出文件时最终发生段错误。


我可以确切地知道为什么。见下文。

在某些时候,我试图用feof()修复这种行为,但没有成功。


猜测很少会解决真正的问题。


功能没有受到伤害,


好吧,不,你不能杀死那些已经死了的东西。 :-)

但这已经开始让我烦恼了。


是的,你有一个严重的致命错误。

我在这里失踪了什么?


您显然忘记查看库

函数的文档,因为您不允许它可能失败。

有时候代码变得令人沮丧!!大声笑!!!


特别是当你试图快速前进时,我怀疑你已经完成了。

代码:

# include< stdio.h>
#include< stdlib.h>
#include< string.h>

int main(int argc,char * argv [] )
{
FILE * fp;

int len;


我在下面看到你在''len''中存储来自''strlen()''
的返回值。这意味着它的类型应该是''size_t'',而不是''int''。

char buf [100];

if((fp = fopen(argv [ 1],r))== NULL){


你应该检查''argv [1]''确实是一个有效的指针

(即确保argc> 1)在尝试取消引用之前。

如果argc< = 1,则表达式''argv [1]'调用

未定义的行为。

fprintf(stderr,不能打开fp);
返回EXIT_FAILURE;
}

while(( (len = strlen(fgets(buf,80,fp)))!= 0)){


如果''fgets()''遇到错误或文件结束,它将返回

NULL。如果你将NULL作为参数传递给''strlen()''你得到

未定义的行为(可以表现为''segfault'')。

printf ("%i \t",len);
printf("%s",buf);
}

fclose(fp); / *不,这里没有错误检查... * /


你也没有做错误检查它真正重要的地方。

检查*的返回值任何*记录的函数

可能会返回''失败''指示(就像'fgets()'')。

返回EXIT_SUCCESS;
}
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well.
No it doesn''t. It invokes undefined behavior.
Except that it (predictably)
segfaults at the end when it tries to go beyond the file.
And I can see exactly why. See below.
At some point, I
tried to mend that behavior using feof() but without success.
Guessing rarely will fix the real problem.
The
functionality is not harmed,
Well, no, you can''t kill something that''s already dead. :-)
but this has started to bug me.
Yes, you have a serious, fatal bug.
What am I
missing here?
You apparently forgot to check the documentation of a library
function, because you didn''t allow for its possible failure.
Sometimes being a code duffer is frustrating!! lol!!!
Especially when you try to go to fast, as I suspect you''ve done.

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
FILE *fp;

int len;
I see below that you store the return value from ''strlen()''
in ''len''. This means its type should be ''size_t'', not ''int''.
char buf[100];

if ((fp = fopen(argv[1], "r")) == NULL) {
You should check that ''argv[1]'' is indeed a valid pointer
(i.e. make sure argc > 1) before trying to dereference it.
If argc <= 1, then the expression ''argv[1]'' invokes
undefined behavior.
fprintf(stderr, "can''t open fp");
return EXIT_FAILURE;
}

while (((len = strlen(fgets(buf, 80, fp))) != 0)) {
If ''fgets()'' encounters an error or end of file, it will return
NULL. If you pass NULL as the argument to ''strlen()'' you get
undefined behavior (which could be manifested as a ''segfault'').
printf(" %i\t", len);
printf("%s", buf);
}

fclose(fp); /* Nah, no error checking here... */
Nor did you do error checking where it really mattered.
Check the return value of *any* function which is documented
to possibly return a ''failure'' indication (as does ''fgets()'').

return EXIT_SUCCESS;
}




-Mike



-Mike


name写道:

当我开始测试我的包装程序的算法时,我把这段代码整合在一起,效果很好。
除了它(可预见地)在最后尝试时会出现段错误。超越文件。在某些时候,我尝试使用feof()来修复这种行为,但没有成功。功能没有受到伤害,但这已经开始让我感到烦恼。我错过了什么
这里?有时候代码变得令人沮丧!!大声笑!!!

代码:

#include< stdio.h>
#include< stdlib.h>
#include< ; string.h>

int main(int argc,char * argv [])
{
FILE * fp;

int len;
char buf [100];

if((fp = fopen(argv [1]," r"))== NULL){
fprintf(stderr,"不能打开fp");
返回EXIT_FAILURE;
}
while(((len = strlen(fgets(buf,80,fp)))!= 0 )){
printf("%i \t",len);
printf("%s",buf);
}

fclose (FP); / *不,这里没有错误检查... * /

返回EXIT_SUCCESS;
}

When I started testing the algorithms for my wrap program, I
threw together this snippet of code, which works quite well.
Except that it (predictably) segfaults at the end when it tries
to go beyond the file. At some point, I tried to mend that
behavior using feof() but without success. The functionality is
not harmed, but this has started to bug me. What am I missing
here? Sometimes being a code duffer is frustrating!! lol!!!

The code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
FILE *fp;

int len;
char buf[100];

if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "can''t open fp");
return EXIT_FAILURE;
}

while (((len = strlen(fgets(buf, 80, fp))) != 0)) {
printf(" %i\t", len);
printf("%s", buf);
}

fclose(fp); /* Nah, no error checking here... */

return EXIT_SUCCESS;
}




查看fgets返回的内容当它遇到文件结尾或

i / o错误时。然后考虑strlen在你要求它咀嚼

时做了什么。


< rant>请删除代码中的过多缩进。

3或4个空格就足够了。过多的空间使得线条太长并导致事物在右边缘消失

(尽管上面的线条足够短以避免这种情况)。不要
使用标签。 < / rant>


-

每次都是正确的男人不太可能做得太多。

- Francis Crick,共同发现DNA

没有什么比行动中的愚蠢更令人惊讶了。

- Thomas Matthews



Look up what fgets returns when it encounters end-of-file or an
i/o error. Then consider what strlen does when you ask it to chew
on that.

<rant> Please get rid of the excessive indentation in your code.
3 or 4 spaces is quite enough. The excessive space makes lines
too long and causes things to disappear over the right margin
(although the above lines are short enough to avoid that). Don''t
use tabs. </rant>

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


CBFalconer写道:
CBFalconer wrote:
查看fgets在遇到文件结尾或
i / o错误时返回的内容。然后考虑一下strlen在你要求它咀嚼时所做的事情。
我已经能够确定fgets何时返回NULL。请你能用

解释一下。

例如,如果我有以下文件:

aaaaaaaa \ n

bbbbbbbbb< EOF>


我会用一个大缓冲区调用fgets三次(lager

然后10个字节或字符) 。什么时候fgets返回NULL?

< rant>请删除代码中的过多缩进。
3或4个空格就足够了。过多的空间使得线条太长并且导致东西在右边缘消失(尽管上面的线条足够短以避免这种情况)。不要使用标签。 < /咆哮>


恕我直言,如果行太长,则需要创建新功能来解决整个任务的部分内容。


" name"写道时(((len = strlen(fgets(buf,80,fp)))!= 0)){
Look up what fgets returns when it encounters end-of-file or an
i/o error. Then consider what strlen does when you ask it to chew
on that. I''ve neve been able to figue out when fgets returns NULL. Please can you
explain it.
Fo example if I''ve got the following file:
aaaaaaaa\n
bbbbbbbbb<EOF>

And I would call fgets three times in a raw with a big buffer (lager
then 10 bytes or chars). When whould fgets return NULL?
<rant> Please get rid of the excessive indentation in your code.
3 or 4 spaces is quite enough. The excessive space makes lines
too long and causes things to disappear over the right margin
(although the above lines are short enough to avoid that). Don''t
use tabs. </rant>
IMHO if lines are too long it''s time to create new function to solve the
part of the whole task.

"name" wrote while (((len = strlen(fgets(buf, 80, fp))) != 0)) {



我认为功能风格足够好,所以我建议你会

写一个strlen包装器。

类似于:

int my_strlen(const char * s)

{

size_t tmp;


if(s == NULL)

返回-1;

tmp = strlen(s);

if(tmp> INT_MAX){

errno = EINVAL;

返回-1 ;

}

return(int)tmp;

}


-

vir


I think that functional style is good enough, so I suggest that you''ll
write a wrapper fo strlen.
Something like:
int my_strlen (const char *s)
{
size_t tmp;

if (s == NULL)
return -1;
tmp = strlen (s);
if (tmp > INT_MAX) {
errno = EINVAL;
return -1;
}
return (int)tmp;
}

--
vir


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

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