bsearch脚本段错误 [英] bsearch script segfault

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

问题描述

我有一个非常简单的脚本,它使用bsearch来查找

特定元素


问题是,它只是在比较中的段错误功能。


我有一个类似的脚本工作正常,现在这只是段错误。

我开车自己疯了,最后4小时,为什么这样一个简单的事情会

不工作


请有人帮忙

谢谢

Ram


#include< stdlib.h>

#include< stdio.h>

#include< string.h>


struct aliasid {

char a_id [100];

int count;

};

int compare_aliases(const void * p1,const void * p2){

const struct aliasid * u1 =(const struct aliasid *)p1;

const struct aliasid ** u2 =(const struct aliasid **)p2;

return(strcmp(u1-> a_id,(* u2) - > a_id));

}


struct aliasid * new_aliasid(char * id,int numids){

struct aliasid * a;

a = malloc(sizeof(struct aliasid)+10);

strcpy(a-> a_id,id );

a-> count = 0;

返回(a);

}


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

struct aliasid ** aliases,** tmp1;

char the_string [100];


别名= malloc(3 * sizeof(void *));

if(别名== NULL){

printf(" malloc失败");

退出(1);

}

别名[0] = new_aliasid(" abc",3);

别名[1] = new_aliasid(" def",3);

别名[2] = NULL;

tmp1 =(struct aliasid **)bsearch(" abc",aliases,2,sizeof(struct

aliasid),compare_aliases);


if(tmp1 == NULL ){

printf(" id%s was found\\\
,the_string);

} else {

printf( id%s被发现了\ n,the_string);


}

返回(0);

}

I have got a pretty simple script , that uses bsearch to look for a
particular element

The problem is , it simply segfaults inside the compare function.

I have a similar script that works fine , and now this simply segfaults.
I am driving myself nuts , last 4 hrs , why such a simple thing would
not work

Pls someone help
Thanks
Ram

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

struct aliasid {
char a_id[100];
int count;
};
int compare_aliases(const void *p1, const void *p2 ){
const struct aliasid *u1 = (const struct aliasid *) p1;
const struct aliasid **u2 = (const struct aliasid **) p2;
return(strcmp(u1->a_id,(*u2)->a_id));
}

struct aliasid* new_aliasid(char* id,int numids) {
struct aliasid* a;
a = malloc(sizeof(struct aliasid) +10);
strcpy(a->a_id,id);
a->count = 0;
return(a);
}

int main(int argc,char* argv[]){
struct aliasid **aliases , **tmp1;
char the_string[100];

aliases = malloc(3 * sizeof(void*));
if(aliases == NULL ) {
printf("malloc failed");
exit(1);
}
aliases[0]= new_aliasid("abc",3);
aliases[1]= new_aliasid("def",3);
aliases[2]=NULL;
tmp1 = (struct aliasid **) bsearch("abc",aliases,2,sizeof( struct
aliasid),compare_aliases);

if(tmp1 == NULL){
printf("The id %s was not found\n",the_string);
} else {
printf("The id %s was found\n",the_string);

}
return(0);
}

推荐答案

Ramprasad A Padmanabhan写道:
Ramprasad A Padmanabhan wrote:
我有一个非常简单的脚本,它使用bsearch来寻找
特殊元素

问题是,它只是在比较函数中的段错误。

我有一个类似的脚本工作正常,现在这只是段错误。我开车自己疯了,最后4小时,为什么这么简单的事情会不起作用

请别人帮忙
谢谢
Ram

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

struct aliasid {
char a_id [100];
int count;
};
int compare_aliases(const void * p1,const void * p2){
const struct aliasid * u1 =(const struct aliasid *)p1;
const struct aliasid ** u2 =(const struct aliasid **)p2;
return(strcmp(u1-> a_id,(* u2) - > a_id)) ;
}

struct aliasid * a;
a = malloc(sizeof(struct aliasid)+10);
strcpy(a-> a_id,id);
a-> count = 0;
返回(a);
}
int main(int argc,char * argv []){
struct aliasid **别名,** tmp1;
char the_string [100];

别名= malloc(3 * sizeof(void *));
if(别名== NULL){
printf(" malloc failed");
exit(1);
}
别名[0] = new_aliasid(" abc",3);
别名[1] = new_aliasid(" def",3);
别名[2] = NULL;
tmp1 =(struct aliasid **)bsearch(" abc",aliases, 2,sizeof(struct
aliasid),compare_aliases);

if(tmp1 == NULL){
printf(" id%s was found\\\
" ,the_string);
} else {
printf(" id%s was found\\\
,the_string);

}
return(0) ;
}
I have got a pretty simple script , that uses bsearch to look for a
particular element

The problem is , it simply segfaults inside the compare function.

I have a similar script that works fine , and now this simply segfaults.
I am driving myself nuts , last 4 hrs , why such a simple thing would
not work

Pls someone help
Thanks
Ram

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

struct aliasid {
char a_id[100];
int count;
};
int compare_aliases(const void *p1, const void *p2 ){
const struct aliasid *u1 = (const struct aliasid *) p1;
const struct aliasid **u2 = (const struct aliasid **) p2;
return(strcmp(u1->a_id,(*u2)->a_id));
}

struct aliasid* new_aliasid(char* id,int numids) {
struct aliasid* a;
a = malloc(sizeof(struct aliasid) +10);
strcpy(a->a_id,id);
a->count = 0;
return(a);
}

int main(int argc,char* argv[]){
struct aliasid **aliases , **tmp1;
char the_string[100];

aliases = malloc(3 * sizeof(void*));
if(aliases == NULL ) {
printf("malloc failed");
exit(1);
}
aliases[0]= new_aliasid("abc",3);
aliases[1]= new_aliasid("def",3);
aliases[2]=NULL;
tmp1 = (struct aliasid **) bsearch("abc",aliases,2,sizeof( struct
aliasid),compare_aliases);

if(tmp1 == NULL){
printf("The id %s was not found\n",the_string);
} else {
printf("The id %s was found\n",the_string);

}
return(0);
}



好​​吧,我想我遇到了问题,


如果我将别名声明为

*别名[3];

那么这个工作正常



这是否意味着我无法动态地为结构分配内存

数组我在bsearch中使用


谢谢

Ram


Ok , I think I have got the problem ,

If I declare aliases as
*aliases[3];
Then this works fine


So does that mean I cannot dynamically allocate memory to a structure
array Which I use in bsearch

Thanks
Ram


Ramprasad A Padmanabhan< ra ******* @ netcore.co.in>写道:
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:
我有一个非常简单的脚本,它使用bsearch来寻找特定元素
问题是,它只是在比较函数中的段错误。
我有一个类似的脚本,工作正常,现在这只是段错误。
我开车自己疯了,最后4小时,为什么这么简单的事情将无法工作
# include< stdlib.h>
#include< stdio.h>
#include< string.h>
struct aliasid {
char a_id [100];
int count;
};
int compare_aliases(const void * p1,const void * p2){
const struct aliasid * u1 =(const struct aliasid *)p1;
const struct aliasid ** u2 =(const struct aliasid **)p2;


为什么p2是指向指针的指针?那不是
很有意义。

return(strcmp(u1-> a_id,(* u2) - > a_id));
}
struct aliasid * new_aliasid(char * id,int numids){
struct aliasid * a;
a = malloc(sizeof(struct aliasid)+10);


为什么要分配比你需要的多10个字节?如果你把它写成


a = malloc(sizeof * a + 10),那么维护(和阅读)的东西将会很轻松。

strcpy(a-> a_id,id);
a-> count = 0;
return(a);
}
int main(int argc,char * argv []){
struct aliasid ** aliases,** tmp1;
char the_string [100];
aliases = malloc(3 * sizeof(void *));


为什么sizeof(void *)?你显然想要一组结构指针。

只需把它写成


别名= malloc(3 * sizeof *别名);

if(别名== NULL){
printf(" malloc failed");
exit(1);
}
别名[0] = new_aliasid(" abc",3);
别名[1] = new_aliasid(" def",3);
别名[2] = NULL;
tmp1 =(struct aliasid **)bsearch( abc,别名,2,sizeof(struct
aliasid),compare_aliases);


这里绝对错误的是''size''参数,即第四个

参数:你传递给bsearch()的是一个数组*指针*,而不是
。使'sizeof *别名''你应该没事。


你不需要演员,就像malloc()一样,它只会保持

编译来自抱怨,如果你忘了包含< stdlib.h>

if(tmp1 == NULL){
printf(" id%s未找到) \ n",the_string);
} else {
printf(" id%s was found\\\
,the_string);


''the_string''从未被赋值,所以你不能打印它。

}
返回(0) ;


难道你不能自己清理并释放你分配的记忆;-)

}
I have got a pretty simple script , that uses bsearch to look for a
particular element The problem is , it simply segfaults inside the compare function. I have a similar script that works fine , and now this simply segfaults.
I am driving myself nuts , last 4 hrs , why such a simple thing would
not work #include <stdlib.h>
#include <stdio.h>
#include <string.h> struct aliasid {
char a_id[100];
int count;
};
int compare_aliases(const void *p1, const void *p2 ){
const struct aliasid *u1 = (const struct aliasid *) p1;
const struct aliasid **u2 = (const struct aliasid **) p2;
Why do you have p2 being a pointer to a pointer? That doesn''t
make much sense.
return(strcmp(u1->a_id,(*u2)->a_id));
} struct aliasid* new_aliasid(char* id,int numids) {
struct aliasid* a;
a = malloc(sizeof(struct aliasid) +10);
Why do you allocate 10 more bytes than you need? And things would be a
lot easer to maintain (and to read) if you would write this as

a = malloc( sizeof *a + 10 );
strcpy(a->a_id,id);
a->count = 0;
return(a);
} int main(int argc,char* argv[]){
struct aliasid **aliases , **tmp1;
char the_string[100]; aliases = malloc(3 * sizeof(void*));
Why sizeof(void*)? You obviously want a array of structure pointers.
Just write it as

aliases = malloc( 3 * sizeof *aliases );
if(aliases == NULL ) {
printf("malloc failed");
exit(1);
}
aliases[0]= new_aliasid("abc",3);
aliases[1]= new_aliasid("def",3);
aliases[2]=NULL;
tmp1 = (struct aliasid **) bsearch("abc",aliases,2,sizeof( struct
aliasid),compare_aliases);
What''s definitely wrong here is the ''size'' argument, i.e. the fourth
argument: what you pass to bsearch() is an array *of pointers*, not
of structures. That''s the most likely reason for the crash you''re
seeing. Make that ''sizeof *aliases'' and you should be fine.

And you don''t need the cast, like for malloc() it will only keep the
compiler from complaining if you forgot to include <stdlib.h>
if(tmp1 == NULL){
printf("The id %s was not found\n",the_string);
} else {
printf("The id %s was found\n",the_string);
''the_string'' never has been assigned a value, so you can''t print it.
}
return(0);
Shouldn''t you clean up after yourself and free the memory you allocated;-)
}



问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.physik.fu-berlin.de/~toerring


Ramprasad A Padmanabhan< ra ******* @ netcore.co.in>这样说:
Ramprasad A Padmanabhan <ra*******@netcore.co.in> spoke thus:
我有一个非常简单的脚本,它使用bsearch寻找
I have got a pretty simple script , that uses bsearch to look for a





-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



<pedantry>A C program isn''t technically a "script," is it?</pedantry>

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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