delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/08/11:45:54

Message-ID: <002301beb1c6$025023c0$4d806ec3@marte>
From: "Giuseppe Santonocito" <incom AT mclink DOT it>
To: <djgpp AT delorie DOT com>
Subject: Bug report
Date: Tue, 8 Jun 1999 17:45:58 +0200
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Reply-To: djgpp AT delorie DOT com

I think I found a bug in the qsort() function.
A very simple test program follows.

GCC version:     2.8.1 (DJGPP DOS porting)
Compiler option: -g
Computer:        Pentium 200 MMX 64MB running a DOS window from within Win95
IDE:             RH IDE

Thank you in advance for any help and best regards.

Giuseppe Santonocito
Via della Manifattura 7 - 50058 Signa (FI) - Italy
incom AT mclink DOT it


//
// bug in qsort() ?
// ----------------
//
// this program will produce a file OUT.TXT that is not correctly
// ordered: elements of index 0 and 1499 are swapped.
//

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

void *array[2000];
int cmp_func( const void *, const void * );

main()
{
   int i;
   FILE *f;

   // fill in the array
   for( i=0; i<2000; i++ )
      array[i] = (void *) i;

   // sort the array
   qsort( array, 2000, sizeof(void *), cmp_func );

   // write the array to a file
   f = fopen( "OUT.TXT", "wt" );
   for( i=0; i<2000; i++ )
      fprintf( f, "%4d - %d\n", i, array[i]);
   fclose( f );
}

int cmp_func( const void * p1, const void * p2 )
{
   // the actual cmp_func() in the program that revealed the bug
   // also returns +1 and -1, of course; but it may happen that
   // an array is already ordered and so always returning 0
   return 0;
}


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019