The functions mentioned so far in this chapter are for searching in a sorted or unsorted array. There are other methods to organize information which later should be searched. The costs of insert, delete and search differ. One possible implementation is using hashing tables. The following functions are declared in the header file search.h.
hcreatefunction creates a hashing table which can contain at least nel elements. There is no possibility to grow this table so it is necessary to choose the value for nel wisely. The method used to implement this function might make it necessary to make the number of elements in the hashing table larger than the expected maximal number of elements. Hashing tables usually work inefficiently if they are filled 80% or more. The constant access time guaranteed by hashing can only be achieved if few collisions exist. See Knuth's “The Art of Computer Programming, Part 3: Searching and Sorting” for more information.
The weakest aspect of this function is that there can be at most one hashing table used through the whole program. The table is allocated in local memory out of control of the programmer. As an extension the GNU C library provides an additional set of functions with an reentrant interface which provide a similar interface but which allow to keep arbitrarily many hashing tables.
It is possible to use more than one hashing table in the program run if the former table is first destroyed by a call to
The function returns a non-zero value if successful. If it return zero something went wrong. This could either mean there is already a hashing table in use or the program runs out of memory.
hdestroyfunction can be used to free all the resources allocated in a previous call of
hcreate. After a call to this function it is again possible to call
hcreateand allocate a new table with possibly different size.
It is important to remember that the elements contained in the hashing table at the time
hdestroyis called are not freed by this function. It is the responsibility of the program code to free those strings (if necessary at all). Freeing all the element memory is not possible without extra, separately kept information since there is no function to iterate through all available elements in the hashing table. If it is really necessary to free a table and all elements the programmer has to keep a list of all table elements and before calling
hdestroys/he has to free all element's data using this list. This is a very unpleasant mechanism and it also shows that this kind of hashing tables is mainly meant for tables which are created once and used until the end of the program run.
Entries of the hashing table and keys for the search are defined using this type:
Both elements of this structure are pointers to zero-terminated strings. This is a limiting restriction of the functionality of the
hsearchfunctions. They can only be used for data sets which use the NUL character always and solely to terminate the records. It is not possible to handle general binary data.
- Pointer to a zero-terminated string of characters describing the key for the search or the element in the hashing table.
- Pointer to a zero-terminated string of characters describing the data. If the functions will be called only for searching an existing entry this element might stay undefined since it is not used.
To search in a hashing table created using
hsearchfunction must be used. This function can perform simple search for an element (if action has the
FIND) or it can alternatively insert the key element into the hashing table. Entries are never replaced.
The key is denoted by a pointer to an object of type
ENTRY. For locating the corresponding position in the hashing table only the
keyelement of the structure is used.
If an entry with matching key is found the action parameter is irrelevant. The found entry is returned. If no matching entry is found and the action parameter has the value
FINDthe function returns a
NULLpointer. If no entry is found and the action parameter has the value
ENTERa new entry is added to the hashing table which is initialized with the parameter item. A pointer to the newly added entry is returned.
As mentioned before the hashing table used by the functions described so
far is global and there can be at any time at most one hashing table in
the program. A solution is to use the following functions which are a
GNU extension. All have in common that they operate on a hashing table
which is described by the content of an object of the type
hsearch_data. This type should be treated as opaque, none of its
members should be changed directly.
hcreate_rfunction initializes the object pointed to by htab to contain a hashing table with at least nel elements. So this function is equivalent to the
hcreatefunction except that the initialized data structure is controlled by the user.
This allows having more than one hashing table at one time. The memory necessary for the
struct hsearch_dataobject can be allocated dynamically. It must be initialized with zero before calling this function.
The return value is non-zero if the operation was successful. If the return value is zero, something went wrong, which probably means the programs ran out of memory.
hdestroy_rfunction frees all resources allocated by the
hcreate_rfunction for this very same object htab. As for
hdestroyit is the programs responsibility to free the strings for the elements of the table.
hsearch_rfunction is equivalent to
hsearch. The meaning of the first two arguments is identical. But instead of operating on a single global hashing table the function works on the table described by the object pointed to by htab (which is initialized by a call to
Another difference to
hcreateis that the pointer to the found entry in the table is not the return value of the functions. It is returned by storing it in a pointer variables pointed to by the retval parameter. The return value of the function is an integer value indicating success if it is non-zero and failure if it is zero. In the latter case the global variable errno signals the reason for the failure.
- The table is filled and
hsearch_rwas called with an so far unknown key and action set to
- The action parameter is
FINDand no corresponding element is found in the table.