This is a reported bug in SysBinarySearch although I have never heard
anything back about whether it has been fixed.

 -----Original Message-----
From:   Thomas Ward [mailto:[EMAIL PROTECTED]]
Sent:   Thursday, January 20, 2000 14:13
To:     [EMAIL PROTECTED]
Cc:     [EMAIL PROTECTED]
Subject:        SysBinarySearch and search function

Hi All,

I am getting some very strange behavior from SysBinarySearch. Specifically,
about half the time it seems to return one greater than the correct answer.
That is, the element I am searching for is in array[20] and yet
SysBinarySearch returns 21. The rest of the time it gives the correct
answer. Even more confusing is that it doesn't seem to happen on all
versions of the OS.

Is there some issue with SysBinarySearch? Did it get "fixed" in some OS
release so that it works correctly only on that release and thereafter? In
case it matters, here are the details of what I am doing:

Could this possibly have something to do with the alignment of my
structure?:

typedef struct {
   ULong         id;
   int           order;
   } sort_element;

These are my variable declarations:

sort_element     *pp_array;     // the array I am searching
int              count;         // # of elements in the array
ULong            id;            // the thing I am searching for
ULong            searchpos;     // the answer from SysBinarySearch

pp_array = MemPtrNew(count * sizeof(sort_element));     // allocate array

static int search_routine(const VoidPtr search_item,
                          const VoidPtr array_item, Long other) {
   ULong  search, array;
   Short  answer;
CALLBACK_PROLOGUE
   search = *((ULong *) search_item);
   array = ((sort_element *) array_item)->id;
   answer = (search < array) ? -1 : (search > array) ? 1 : 0;
CALLBACK_EPILOGUE
   return answer;
   }

This is how I do my search:

if (SysBinarySearch(pp_array, (UInt) count, (UInt) sizeof(sort_element),
      (SearchFuncPtr) &search_routine, &id, (ULong) 0, &searchpos, false))
...

I can fix my problem by doing this:

   if (pp_array[searchpos].id != id) searchpos--;       // are we one too far?

right after the search, but I really would rather understand what's going
on. Thanks in advance!

Tom Ward

Reply via email to