[comp.std.c] bsearch of empty array

kers@hplb.hpl.hp.com (Chris Dollin) (05/15/91)

If bsearch is called with $nmemb == 0$, is $base$ required to point to a
legitimate array, or can it be $NULL$ ?

[A library I know of hangs in this case; I'd like to know if my code is broken,
or if the library is.]

A similar question arises for $qsort$; if one $qsort$'s 0 elements, is the
value of the array pointer required to point to a proper array, or is it
allowed to be $NULL$?
--

Kers.       | "A foolish hobgoblin has the consistency of little minds".

gwyn@smoke.brl.mil (Doug Gwyn) (05/16/91)

In article <KERS.91May15154937@cdollin.hpl.hp.com> kers@hplb.hpl.hp.com (Chris Dollin) writes:
>If bsearch is called with $nmemb == 0$, is $base$ required to point to a
>legitimate array, or can it be $NULL$ ?

In general, when the C standard requires an argument to a library function
to be a pointer to an array of XXX, it means it.  Null pointers are allowed
only where specified.

kers@hplb.hpl.hp.com (Chris Dollin) (05/16/91)

Doug Gwyn says (replying to my bsearch question):

   In general, when the C standard requires an argument to a library function
   to be a pointer to an array of XXX, it means it.  Null pointers are allowed
   only where specified.

Thanks, Doug. I don't like it (in the case I described), but I can live with
it. I've now read 4.1.6 and found the bit that outlaws $NULL$.

Now, which implementations have better ``quality'' - those that are happy with
a null pointer and no elements (where the meaning is ``obvious''), or those
that object (and hence force your code to be more portable) .... :-)
--

Regards, Kers.      | "You're better off  not dreaming of  the things to come;
Caravan:            | Dreams  are always ending  far too soon."

gwyn@smoke.brl.mil (Doug Gwyn) (05/17/91)

In article <KERS.91May16122021@cdollin.hpl.hp.com> kers@hplb.hpl.hp.com (Chris Dollin) writes:
>Now, which implementations have better ``quality'' - those that are happy with
>a null pointer and no elements (where the meaning is ``obvious''), or those
>that object (and hence force your code to be more portable) .... :-)

Well, it's a matter of opinion.  The best implementation for some of us
(who are trying to develop portable software) would be one that detects
the nonportable usage (null pointer argument) and makes an appropriate
squawking sound, so we can fix our programs now rather than much later
when the bug causes mysterious behavior on another system.  Others would
say that the implementation should not waste any cycles testing for
things that it is not required to handle.  Still others would agree with
you that it would be nice for the implementation to accept this usage.