From owner-freebsd-bugs Mon Sep 16 11:30: 5 2002 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3CB4A37B400 for ; Mon, 16 Sep 2002 11:30:04 -0700 (PDT) Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id EB21143E6E for ; Mon, 16 Sep 2002 11:30:03 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.4/8.12.4) with ESMTP id g8GIU3JU013034 for ; Mon, 16 Sep 2002 11:30:03 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.4/8.12.4/Submit) id g8GIU3YE013033; Mon, 16 Sep 2002 11:30:03 -0700 (PDT) Date: Mon, 16 Sep 2002 11:30:03 -0700 (PDT) Message-Id: <200209161830.g8GIU3YE013033@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org Cc: From: "Jin Guojun [DSD]" Subject: Re: kern/42818: qsort can only sort up to 6 element for structure Reply-To: "Jin Guojun [DSD]" Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org The following reply was made to PR kern/42818; it has been noted by GNATS. From: "Jin Guojun [DSD]" To: Gregory Bond Cc: FreeBSD-gnats-submit@FreeBSD.ORG Subject: Re: kern/42818: qsort can only sort up to 6 element for structure Date: Mon, 16 Sep 2002 11:26:29 -0700 My fault -- I typed a wrong operator "<". It supposes to be "-". So, the correct operation is: int smaller(const void *i1, const void *i2) { return *(int*)i1 - *(int*)i2; } Thanks, -Jin Gregory Bond wrote: > The bug is in your program. To quote qsort(3): > > The comparison function must return an integer less than, equal to, or > greater than zero if the first argument is considered to be respectively > less than, equal to, or greater than the second. > > Your comparison function returns 0 or 1 only. Perhaps you are being confused > by the C++/STL comparison objects? > > Modifying your comparison fn to the following makes it work: > int > smaller(const void *i1, const void *i2) > { > if (*(int*)i1 < *(int*)i2) return -1; > return *(int*)i1 > *(int*)i2; > } > > The fact that your test program seems to work for these test cases on Solaris > and Linux is a simple coincidence, and depends on fine internal details of the > sort algorithm on those systems. There will probably be other cases where the > Linux/Solaris qsort() will also fail. > > This PR can be closed. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message