为什么在for循环中segfault和no匹配? [英] Why segfault and no NULL match in for loop?

查看:71
本文介绍了为什么在for循环中segfault和no匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有两个文件名为search.c和search.h。

在search.c的for循环中,for循环永不退出,即使是
mystruct [i] .field1无法匹配。它不会退出for循环而是继续运行直到它出现段错误。

这似乎与带有NULL

值的strcmp有关。下面有2条评论表明段错误是
。我想问题是,当那里没有匹配时,我怎么检测到并返回没有

a段错误?


-Thanks

SEARCH.C ------------------------------------- ---


#include< string.h>

#include< stdlib.h>

#include< ; stdio.h>


#include" search.h"

int search(char * field1)

{

int i = 0;

/ *这是永远=到NULL,即使没有匹配field1 * /


for (i = 0; mystruct [i] .field1!= NULL; i ++){


if(strcmp(field1,mystruct [i] .field1)== 0){

返回(mystruct [i] .numfield);

}

/ *这个SEGFAULTS总是!!!! * /

/ * if(strcmp(mystruct [i] .field1,NULL)== 0){* /

/ *} * /


}


返回(0);

}


int main(无效)

{

int retval = 0;


/ *这可能导致SEGFAULT TOO ! * /

/ *如果你匹配,例如CCC,这是有效的。但是如果* /

/ *你将CCC更改为YYY,这不在* /

/ *结构中,那就是段错误。* /


char str [] =" CCC";


retval = search(str);


printf (" retval =%d \ n",retval);


返回0;

}

SEARCH.H -----------------------------

struct _mystruct

{

char field1 [4];

char field2 [4];

char field3 [2];

char field4 [3];

int numfield;

};

struct _mystruct mystruct [] =

{

AAA,EEE,R,DF,25,

BBB,FFF,R和R。 ,DF,25,

CCC,GGG,R,DF,99,

" DDD" ,HHH,R,DF,13,

};

There are two files below named search.c and search.h.
In the for loop in search.c, the for loop never exits,
even if mystruct[i].field1 has no match. Instead of
exiting the for loop it keeps going until it segfaults.
This seems to be related to the strcmp with the NULL
value. There are 2 comments below that indicate
the segfaults. I guess the question is, when there
is no match, how to I detect that and return without
a segfault?

-Thanks
SEARCH.C ----------------------------------------

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

#include "search.h"
int search(char * field1)
{
int i = 0;
/* This is never = to NULL, even when no match for field1 */

for(i=0; mystruct[i].field1 != NULL; i++) {

if(strcmp(field1, mystruct[i].field1) == 0) {
return(mystruct[i].numfield);
}
/*THIS SEGFAULTS ALWAYS!!!!*/
/*if (strcmp(mystruct[i].field1, NULL) == 0) {*/
/*}*/

}

return(0);
}

int main(void)
{
int retval = 0;

/* THIS CAN CAUSE SEGFAULT TOO!!! */
/*This works if you match, for example, CCC. But if*/
/*you change the CCC to say YYY, which is not in the*/
/*struct, it segfaults.*/

char str[] = "CCC";

retval = search(str);

printf("retval = %d\n", retval);

return 0;
}
SEARCH.H -----------------------------
struct _mystruct
{
char field1[4];
char field2[4];
char field3[2];
char field4[3];
int numfield;
};
struct _mystruct mystruct[] =
{
"AAA", "EEE", "R", "DF", 25,
"BBB", "FFF", "R", "DF", 25,
"CCC", "GGG", "R", "DF", 99,
"DDD", "HHH", "R", "DF", 13,
};

推荐答案

In articl e< Xr ****************************** @ comcast.com> ;,

somebody< ;所以** @ body.comwrote:
In article <Xr******************************@comcast.com>,
somebody <so**@body.comwrote:

>下面有两个文件名为search.c和search.h。
在for循环中search.c,for循环永不退出,
即使mystruct [i] .field1没有匹配。它不会退出for循环而是继续运行直到它出现段错误。
这似乎与具有NULL值的strcmp有关。
>There are two files below named search.c and search.h.
In the for loop in search.c, the for loop never exits,
even if mystruct[i].field1 has no match. Instead of
exiting the for loop it keeps going until it segfaults.
This seems to be related to the strcmp with the NULL
value.


> int search(char * field1)

int i = 0;
>int search(char * field1)
{
int i = 0;


/ *即使没有匹配field1 * /
/* This is never = to NULL, even when no match for field1 */

,这也永远不会为NULL
for(i = 0; mystruct [i] .field1!= NULL; i ++){
for(i=0; mystruct[i].field1 != NULL; i++) {



如果你检查你的mystruct数据,你会看到你没有

有任何mystruct [< anything>] == NULL的条目。

你因此迭代mystruct []的结尾,并开始

测试一个不存在的mystruct [i] .field1条目。

If you examine your mystruct data, you will see that you do not
have any entries in which mystruct[<anything>] == NULL .
You therefore iterate off the end of mystruct[], and start
testing a non-existant mystruct[i].field1 entry.


if(strcmp(field1,mystruct [i]。 field1)== 0){

return(mystruct [i] .numfield);

}
if(strcmp(field1, mystruct[i].field1) == 0) {
return(mystruct[i].numfield);
}


}

return(0);
}
}
return(0);
}


> struct _mystruct
{

char field1 [4];

char field2 [4];

char field3 [2];

char field4 [3];

int numfield;
};

struct _mystruct mystruct [] =
{

AAA,EEE,R,DF,25,

BBB,FFF,R, DF,25,

CCC,GGG,R,DF,99,

" DDD"," HHH,R,DF,13,
};
>struct _mystruct
{
char field1[4];
char field2[4];
char field3[2];
char field4[3];
int numfield;
};
struct _mystruct mystruct[] =
{
"AAA", "EEE", "R", "DF", 25,
"BBB", "FFF", "R", "DF", 25,
"CCC", "GGG", "R", "DF", 99,
"DDD", "HHH", "R", "DF", 13,
};



-

编程是在你忙于制定其他计划的时候发生的事情。


--
Programming is what happens while you''re busy making other plans.


在文章< f1 ********** @ canopus.cc.umanitoba.ca>中,

Walter Roberson< ro ****** @ ibd.nrc-cnrc.gc.cawrote:
In article <f1**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:

> / *这永远不会= NULL,即使没有匹配field1 * /
> /* This is never = to NULL, even when no match for field1 */


> for(i = 0; mystruct [i] .field1!= NULL; i ++){
> for(i=0; mystruct[i].field1 != NULL; i++) {


>如果检查你的mystruct数据,你会看到你没有任何mystruct [< anything>] == NULL的条目。
你因此迭代mystruct []的结尾,并开始
测试一个不存在的mystruct [i] .field1条目。
>If you examine your mystruct data, you will see that you do not
have any entries in which mystruct[<anything>] == NULL .
You therefore iterate off the end of mystruct[], and start
testing a non-existant mystruct[i].field1 entry.



由于mystruct是一个数组,你可以循环它已知的

长度:


for(i = 0; i< sizeof(mystruct)/ sizeof(mystruct [0]); i ++)

And since mystruct is an array, you can just loop over its known
length:

for(i=0; i<sizeof(mystruct)/sizeof(mystruct[0]); i++)


> ;> struct _mystruct
>>struct _mystruct



不要使用以下划线开头的名字,它们是保留的。


- Richard

-

在一些字母表中需要考虑多达32个字符

" ; - X3.4,1963。

Don''t use names starting with underscore, they''re reserved.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


有人写道:
somebody wrote:

下面有两个名为search.c的文件和search.h。

在search.c中的for循环中,即使mystruct [i] .field1没有匹配,for循环也永远不会退出,

。它不会退出for循环而是继续运行直到它出现段错误。

这似乎与带有NULL

值的strcmp有关。下面有2条评论表明段错误是
。我想问题是,当那里没有匹配时,我怎么检测到并返回没有

a段错误?


-Thanks


SEARCH.C --------------------------------- -------


#include< string.h>

#include< stdlib.h>

#include< stdio.h>


#include" search.h"


int search(char * field1)

{

int i = 0;


/ *即使没有匹配field1 * / <,这也永远不会为NULL br $> b $ b for(i = 0; mystruct [i] .field1!= NULL; i ++){
There are two files below named search.c and search.h.
In the for loop in search.c, the for loop never exits,
even if mystruct[i].field1 has no match. Instead of
exiting the for loop it keeps going until it segfaults.
This seems to be related to the strcmp with the NULL
value. There are 2 comments below that indicate
the segfaults. I guess the question is, when there
is no match, how to I detect that and return without
a segfault?

-Thanks
SEARCH.C ----------------------------------------

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

#include "search.h"
int search(char * field1)
{
int i = 0;
/* This is never = to NULL, even when no match for field1 */

for(i=0; mystruct[i].field1 != NULL; i++) {



你的条件是什么时候?永远都是真的吗?那些字符数组可以

永远不会为NULL。您需要添加另一个空白数组输入,测试

为空字符串或其他东西。


for(i = 0; mystruct [i] .field1 [0]!=' '\'''; i ++){

When will your conditional ever be true? Those character arrays can
NEVER be NULL. You need to add another "blank" array entry, and test
for an empty string or something.

for(i=0; mystruct[i].field1[0] != ''\0''; i++) {


>

if(strcmp(field1,mystruct [i] .field1)== 0){

return(mystruct [i] .numfield);

}


/ * THIS SEGFAULTS总是!!!! * /

/ * if(strcmp(mystruct [i] .field1,NULL)== 0){* /

/ *} * /
>
if(strcmp(field1, mystruct[i].field1) == 0) {
return(mystruct[i].numfield);
}
/*THIS SEGFAULTS ALWAYS!!!!*/
/*if (strcmp(mystruct[i].field1, NULL) == 0) {*/
/*}*/



嗯,是的。 NULL不是一个字符串,并且将空指针传递给大多数

字符串处理例程会导致未定义的行为。要测试一个

空指针(你不能拥有),请使用相等。

Well, yeah. NULL is not a string, and passing a null pointer to most of
the string-handling routines causes undefined behavior. To test for a
null pointer (which you can''t have), use equality.


struct _mystruct

{

char field1 [4];

char field2 [4];

char field3 [2];

char field4 [3];

int numfield;

};
struct _mystruct
{
char field1[4];
char field2[4];
char field3[2];
char field4[3];
int numfield;
};



尝试添加:

Try adding:


struct _mystruct mystruct [] =

{

" AAA"," EEE"," ; R,DF,25,

BBB,FFF,R,DF,25,

" ; CCC,GGG,R,DF,99,

DDD,HHH,R,DF,13 ,
struct _mystruct mystruct[] =
{
"AAA", "EEE", "R", "DF", 25,
"BBB", "FFF", "R", "DF", 25,
"CCC", "GGG", "R", "DF", 99,
"DDD", "HHH", "R", "DF", 13,



"","","","",0

"", "", "", "", 0


};
};




Brian



Brian


这篇关于为什么在for循环中segfault和no匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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