Re: [PHP4BETA] (Fwd) Re: [PHP4BETA] cvs: /php4 ChangeLog /php4/ext/standard basic_functions.c basic_functions.h

From: Date: Fri, 08 Oct 1999 15:11:32 +0000
Subject: Re: [PHP4BETA] (Fwd) Re: [PHP4BETA] cvs: /php4 ChangeLog /php4/ext/standard basic_functions.c basic_functions.h
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-5397@lists.php.net to get a copy of this message
Rasmus Lerdorf wrote: > qsort() on Solaris seems pretty broken to me. When you feed it a > non-causal comparison function, it sometimes screws up. > > For example, try this test broken on both Linux and Solaris: > > #include <stdio.h> > #include <stdlib.h> > #include <time.h> > > static int cmp(const void *a, const void *b) { > return( (rand()%2) ? -1 : 1 ); > } > > int main(void) { > int i,l; > char *foo = malloc(32); > > srand(time(NULL)); > for(i=0;i<1000;i++) { > strcpy(foo,"AAABBBAAABBBAAABBBAAABBB"); > l=strlen(foo); > qsort(foo,l,1,cmp); > if(strlen(foo)!=l) printf("Broken!\n"); > else printf("%s\n",foo); > } > } > > Run it a few times and grep its output for "Broken". You will notice that > it never says Broken on Linux but just about always has a couple of Broken > lines on Solaris. -- End of excerpt from Rasmus Lerdorf This happens because Solaris uses proper qsort(), whereas Linux's qsort() is really a mergesort. qsort() expects: a cmp b = -(b cmp a) That is, deterministically, the result of comparison of b and a has to be the opposite of a and b. Anything else and qsort() breaks. -Andrei * If it ain't broken, it doesn't have enough features yet. *

« previous php.version4 (#5397) next »