Bug#279044: libc6: qsort() doesn't sort for large size
Package: libc6
Version: 2.3.2.ds1-18
Severity: normal
$ cat mysort.c
#include <stdio.h>
#include <stdlib.h>
#define DATASIZE 1800
#define NDAT 100000
typedef struct {
int key;
char data [DATASIZE];
} mydata;
int mycomp(void const *p1, void const *p2) {
mydata const *v1, *v2;
v1 = p1;
v2 = p2;
return (v1->key) < (v2->key);
}
int main() {
int i;
mydata *p;
p = calloc(NDAT, sizeof(mydata));
for (i=0; i<NDAT; i++) {
p[i].key = random();
}
qsort(p, NDAT, sizeof(*p), mycomp);
for (i=0; i<10; i++) {
printf("%d\n",p[i].key);
}
return 0;
}
$ gcc -g -Wall -o mysort mysort.c
$ ./mysort
1804289383
585681566
200863687
1979603839
2085853640
1489258466
556712446
1788149182
1893664349
43517041
which is pretty unsorted, if you ask me :-)
It's OK for a smaller struct, though:
$ cat mysort-works.c
#include <stdio.h>
#include <stdlib.h>
#define DATASIZE 20
#define NDAT 100000
typedef struct {
int key;
char data [DATASIZE];
} mydata;
int mycomp(void const *p1, void const *p2) {
mydata const *v1, *v2;
v1 = p1;
v2 = p2;
return (v1->key) < (v2->key);
}
int main() {
int i;
mydata *p;
p = calloc(NDAT, sizeof(mydata));
for (i=0; i<NDAT; i++) {
p[i].key = random();
}
qsort(p, NDAT, sizeof(*p), mycomp);
for (i=0; i<10; i++) {
printf("%d\n",p[i].key);
}
return 0;
}
$ gcc -g -Wall -o mysort-works mysort-works.c
$ ./mysort-works
2147469841
2147466242
2147414141
2147378031
2147317028
2147316088
2147311484
2147278491
2147248040
2147230544
$
-- System Information:
Debian Release: 3.1
APT prefers unstable
APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: i386 (i686)
Kernel: Linux 2.6.8-1-k7
Locale: LANG=C, LC_CTYPE=de_DE@euro (charmap=ISO-8859-15)
Versions of packages libc6 depends on:
ii libdb1-compat 2.1.3-7 The Berkeley database routines [gl
-- no debconf information
Reply to: