Problem
I am using the routine qsort(S) with a large (over 250000 elements of type struct) array. The program hangs at the qsort(S) call although the same code worked fine in earlier releases.
Cause
There is a problem with the qsort(S) library routine in the SCO OpenServer Development System. This can be seen in the following sample:
/* Compile two versions of this to see the problem. * "cc -o qtest qtest.c" should hang at the qsort call * "cc -o qtest.short qtest.c -D SHORT" should execute normally */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>typedef struct {
char name[31];
int key;
char action;
char row[19];
} STRUCTS;static STRUCTS *struct_ptr;#define TABLES 10
#define ACTIONS 23main( int argc, char *argv[] )
{#if SHORT
#define STRUCT_ARRAY_SIZE 250
#else
#define STRUCT_ARRAY_SIZE 250000
#endifstatic STRUCTS structs[STRUCT_ARRAY_SIZE];
STRUCTS *first,*second;int compare_structs();static char *tables[TABLES] = {
"G_TABLE", "CL_TABLE",
"B_TABLE", "G_TABLE",
"O_TABLE", "CR_TABLE",
"R_TABLE", "CODE",
"CA_TABLE", "TEMP_TABLE" };static char actions[] = {
'I','I','U','D','I','D','U','I','I','U',
'I','D','U','I','U','D','D',' ','D','I',
' ','I','U' };static char *row = {"00000333.0000.0001"};int ii = 0,jj = 0,kk=0;/* Initialize the array */
for( struct_ptr=structs; struct_ptr-structs < STRUCT_ARRAY_SIZE; struct_ptr++ ) {
strcpy( struct_ptr->name,tables[(ii++)%TABLES] );
struct_ptr->key = (kk++)%1234;
struct_ptr->action = actions[(jj++)%TABLES];
strcpy( struct_ptr->row, row );
}printf("Print out first 20 structs\n");
for( struct_ptr=structs; struct_ptr-structs < 20; struct_ptr++ )
printf( "%30s %4d %c %s\n",
struct_ptr->name,
struct_ptr->key,
struct_ptr->action,
struct_ptr->row );printf("\nCalling qsort: "); qsort( (void *)structs, STRUCT_ARRAY_SIZE, (size_t)sizeof(structs[0]), compare_structs ); printf("/nReturning from qsort\n");
printf("The first 20 sorted structs are:\n");
for( struct_ptr=structs; struct_ptr-structs < 20; struct_ptr++ )
printf( "%30s %4d %c %s\n",
struct_ptr->name,
struct_ptr->key,
struct_ptr->action,
struct_ptr->row );
}/*-----------------------------------------------------------------*/
int compare_structs(STRUCTS *first, STRUCTS *second)
{
int order;
if( !( order = strcmp(first->name, second->name) ) ) {
if( first->action == second->action )
order = 0;
else if( first->action < second->action )
order = -1;
else
order = 1;
} return(order);
}/*************************************************************************/ Solution
This has been reported to SCO Engineering. As a workaround, the qsort(S) object file from either the SCO UNIX System V/386 Release 3.2 Version 4.2 or SCO Open Desktop Release 3.0 Development System can be used as follows:
1. Extract the qsort.o file from /usr/lib/libc.a:
# ar -x /lib/libc.a qsort.o2. Compile your program using this object file instead of the object file in the OpenServer libc.a:
# cc <filename.c> qsort.o ...