safec  3.0
Safe C Library - ISO TR24731 Bounds Checking Interface
bsearch_s.c File Reference
#include "safe_lib.h"
+ Include dependency graph for bsearch_s.c:

Functions

EXPORT void * bsearch_s (const void *key, const void *base, rsize_t nmemb, rsize_t size, int(*compar)(const void *k, const void *y, void *context), void *context)
 The bsearch_s function finds an element equal to element pointed to by key in an array pointed to by base. More...
 

Function Documentation

◆ bsearch_s()

EXPORT void* bsearch_s ( const void *  key,
const void *  base,
rsize_t  nmemb,
rsize_t  size,
int(*)(const void *k, const void *y, void *context)  compar,
void *  context 
)

The bsearch_s function finds an element equal to element pointed to by key in an array pointed to by base.

The array contains count elements of size bytes and must be partitioned with respect to key, that is, all the elements that compare less than must appear before all the elements that compare equal to, and those must appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using function pointed to by comp. The behavior is undefined if the array is not already partitioned with respect to *key in ascending order according to the same criterion that compar uses.

Remarks
SPECIFIED IN
Parameters
[in]keypointer to the element to search for
[in]basepointer to the array to examine
[in]nmembnumber of elements in the array
[in]sizesize of each element in the array in bytes
[in]comparcomparison function which returns ​a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.
[in]contextadditional information (e.g., collating sequence), passed to compar as the third argument
Precondition
key, base or compar shall not be a null pointer (unless nmemb is zero).
nmemb or size shall not be greater than RSIZE_MAX_MEM.

If the array contains several elements that compar would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result.

Returns
Pointer to an element in the array that compares equal to *key, or null pointer if such element has not been found, or any run-time constraint violation.
Note
Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees. Unlike other bounds-checked functions, bsearch_s does not treat arrays of zero size as a runtime constraint violation and instead indicates element not found (the other function that accepts arrays of zero size is qsort_s).
Return values
NULLwhen not found or any error

errno values: ESNULLP when key, base or compar are a NULL pointer, and nmemb is > 0 ESLEMAX when nmemb or size > RSIZE_MAX_MEM

See also
qsort_s()