在SunOS 5.8上qsort的非标准行为? [英] non-standard behaviour of qsort on SunOS 5.8?

查看:78
本文介绍了在SunOS 5.8上qsort的非标准行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我只需要找出一个在linux下运行良好的程序和/或
True64 alpha在SunOS 5.8下崩溃并出现分段错误。我砸了
,看来如果太阳qsort得到一个
的向量,其中所有元素都是等价的,我的比较函数会增加

SEGSEGV 。


问题似乎是所有元素都是相同的sun qsort

总是将NULL作为comparsion函数的第一个元素传递。

这个标准符合吗?好吧,我想我想念一些东西,所以来吧

a测试:


#include< stdio.h>

# include< stdlib.h>


typedef struct {

int n;

double score;

} DS;


static int cmp(const void * aptr,const void * bptr);


int cmp(const void * aptr,const void * bptr){

DS * a = *((DS **)aptr);

DS * b = *((DS **)bptr );


返回a->得分b->得分? a->得分== b->得分? 0:-1:1;

}


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

DS ** vec;

int i;

int n;

double f;


f = 1.0 /(双)RAND_MAX;

n = argc 0? atoi(argv [1]):5;

vec =(DS **)malloc(n * sizeof(DS *));

for(i = 0; i< n; i ++){

vec [i] =(DS *)malloc(sizeof(DS));

if(argv [2] [0] = ='''''){

vec [i] - >得分= rand()* f;

}否则如果(argv [2] [0] ==''b''){

vec [i] - >得分= 3.123;

}否则if(argv [2] [0] ==' 'c''){

vec [i] - >得分= 3.123;

}

fprintf(stderr," = %f \ n",vec [i] - >得分);

}

if(argv [2] [0] ==''c'' )vec [0] - >得分= 445.6;

qsort(vec,n,sizeof(DS *),cmp);

fputs(" ***排序*** \ n",stderr);

for(i = 0; i< n; i ++){

fprintf(stderr," score =% f \ n",vec [i] - >得分);

}


返回0;

}


然后:


cc -g -o testit testit.c


在sunOS上运行5.8给出:


3个随机值排序

$$ ./testit 3 a

得分= 0.513871

得分= 0.175726

得分= 0.308634

***排序***

得分= 0.513871

得分= 0.308634

得分= 0.175726


3个值(全部相同)已分类

$$ ./testit 3 b

得分= 3.123000

得分= 3.123000

得分= 3.123000

分段错误


3个值(除了之外)都是一样的

$$ ./testit 3 c

得分= 3.123000

得分= 3.123000

得分= 3.123000

***排序***

得分= 445.600000

得分= 3.123000

得分= 3.123000



得分= 3.123000

得分= 3.123000 < br $>
得分= 3.123000


程序接收信号SIGSEGV,分段错误。
$ c $ b 0x0001091c in cmp(aptr = 0x2111c,bptr = 0x21124)在testit。 c:15

15返回a->得分b->得分? a->得分== b->得分? 0:-1:

1;

(gdb)打印一个

$ 1 =(DS *)0x0

(gdb)打印b

$ 2 =(DS *)0x21150

(gdb)


所以指针传递给cmp函数不是NULL,但''a''是NULL

(ig de-referenced aptr)。在我的设置中,我有一个

结构列表,要在得分属性上排序,所以我真的需要一个

指向DS structrue的指针,它发生了有时阵列中只有少数几个元素,并且它们有相同的分数。


我真的被困在这里,任何想法都要去了on,以及为什么其他系统

(win32与gcc,linux与gcc,True64与cc)更宽容?


感谢您的帮助

+亲切的问候,


Arne


ps:我读了 http://c-faq.com/lib/qsort2.html (comp.lang.c常见问题列表·

问题13.9)但似乎仍然忽略了这一点......

Hello,

I just had to find out that a program that runs fine under linux and
True64 alpha crashed with a segmentation fault under SunOS 5.8. I
drilled down and it appears that if the sun qsort gets a vector in
which all elements are equivalent, my comparsion function raises
SEGSEGV.

The problem seems to be that all elements are the same sun qsort
always passes NULL as the first element to the comparsion function. Is
this standard conform ? Well, I guess I miss something, so here comes
a test:

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

typedef struct {
int n ;
double score;
} DS;

static int cmp(const void *aptr, const void *bptr);

int cmp(const void *aptr, const void *bptr) {
DS *a = *((DS**)aptr);
DS *b = *((DS**)bptr);

return a->score b->score ? a->score == b->score ? 0 : -1 : 1;
}

int main(int argc, char *argv[]) {
DS** vec;
int i;
int n;
double f;

f = 1.0/(double)RAND_MAX;
n = argc 0 ? atoi(argv[1]) : 5;
vec = (DS**)malloc(n * sizeof(DS*));
for ( i=0; i<n; i++ ) {
vec[i] = (DS*)malloc(sizeof(DS));
if ( argv[2][0] == ''a'' ) {
vec[i]->score = rand()*f;
} else if ( argv[2][0] == ''b'' ) {
vec[i]->score = 3.123;
} else if ( argv[2][0] == ''c'' ) {
vec[i]->score = 3.123;
}
fprintf(stderr, "score = %f\n", vec[i]->score);
}
if ( argv[2][0] == ''c'' ) vec[0]->score = 445.6;
qsort(vec, n, sizeof(DS*), cmp);
fputs("*** sorted *** \n", stderr);
for ( i=0; i<n; i++ ) {
fprintf(stderr, "score = %f\n", vec[i]->score);
}

return 0;
}

Then:

cc -g -o testit testit.c

running this on sunOS 5.8 gives:

3 random values are sorted
$$ ./testit 3 a
score = 0.513871
score = 0.175726
score = 0.308634
*** sorted ***
score = 0.513871
score = 0.308634
score = 0.175726

3 values (all the same) are sorted
$$ ./testit 3 b
score = 3.123000
score = 3.123000
score = 3.123000
Segmentation Fault

3 values (all but but) are the same
$$ ./testit 3 c
score = 3.123000
score = 3.123000
score = 3.123000
*** sorted ***
score = 445.600000
score = 3.123000
score = 3.123000

in gdb I found out that:

score = 3.123000
score = 3.123000
score = 3.123000

Program received signal SIGSEGV, Segmentation fault.
0x0001091c in cmp (aptr=0x2111c, bptr=0x21124) at testit.c:15
15 return a->score b->score ? a->score == b->score ? 0 : -1 :
1;
(gdb) print a
$1 = (DS *) 0x0
(gdb) print b
$2 = (DS *) 0x21150
(gdb)

so the pointer passed to the cmp function are not NULL, but ''a'' is NULL
(i.g. the de-referenced aptr). In my situtqtion I have a list of
structure to be sorted on the ''score'' attribute, so I realy need a
pointer to DS structrue, and it happenes that sometimes there are only
a few elements in the array and they have the same score.

I realy got stuck here, any ideas wha''s going on, and why other systems
(win32 with gcc, linux with gcc, True64 with cc) are more forgiving?

thanks for your help
+kind regards,

Arne

ps: I read http://c-faq.com/lib/qsort2.html (comp.lang.c FAQ list ·
Question 13.9) but still seem to miss the point ...

推荐答案

./ testit 3 a

得分= 0.513871

得分= 0.175726

得分= 0.308634

***排序***
得分= 0.513871

得分= 0 .308634

得分= 0.175726

3个值(全部相同)已分类
./testit 3 a
score = 0.513871
score = 0.175726
score = 0.308634
*** sorted ***
score = 0.513871
score = 0.308634
score = 0.175726

3 values (all the same) are sorted


。 / testit 3 b

得分= 3.123000

得分= 3.123000

得分= 3.123000

分段错误

3个值(除了之外)都是相同的
./testit 3 b
score = 3.123000
score = 3.123000
score = 3.123000
Segmentation Fault

3 values (all but but) are the same


./ testit 3 c

得分= 3.123000

得分= 3.123000

得分= 3.123000

***排序***

得分= 445.600000

得分= 3.123000

得分= 3.123000


in gdb我发现:


得分= 3.123000

得分= 3.123000

得分= 3.123000


程序收到信号SIGSEGV,分段错误。
在testit.c的cmp(aptr = 0x2111c,bptr = 0x21124)中
0x0001091c:15

15返回a->得分b->得分? a->得分== b->得分? 0:-1:

1;

(gdb)打印
./testit 3 c
score = 3.123000
score = 3.123000
score = 3.123000
*** sorted ***
score = 445.600000
score = 3.123000
score = 3.123000

in gdb I found out that:

score = 3.123000
score = 3.123000
score = 3.123000

Program received signal SIGSEGV, Segmentation fault.
0x0001091c in cmp (aptr=0x2111c, bptr=0x21124) at testit.c:15
15 return a->score b->score ? a->score == b->score ? 0 : -1 :
1;
(gdb) print a


这篇关于在SunOS 5.8上qsort的非标准行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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