2 .\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de)
3 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
6 .\" SPDX-License-Identifier: GPL-2.0-or-later
8 .\" References consulted:
9 .\" SunOS 4.1.1 man pages
10 .\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
11 .\" Remarks from dhw@gamgee.acad.emich.edu Fri Jun 19 06:46:31 1998
12 .\" Modified 2001-12-26, 2003-11-28, 2004-05-20, aeb
13 .\" 2008-09-02, mtk: various additions and rewrites
14 .\" 2008-09-03, mtk, restructured somewhat, in part after suggestions from
15 .\" Timothy S. Nelson <wayland@wayland.id.au>
17 .TH hsearch 3 (date) "Linux man-pages (unreleased)"
19 hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r,
20 hsearch_r \- hash table management
23 .RI ( libc ", " \-lc )
26 .B #include <search.h>
28 .BI "int hcreate(size_t " nel );
29 .B "void hdestroy(void);"
31 .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action );
33 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
34 .B #include <search.h>
36 .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" htab );
37 .BI "void hdestroy_r(struct hsearch_data *" htab );
39 .BI "int hsearch_r(ENTRY " item ", ACTION " action ", ENTRY **" retval ,
40 .BI " struct hsearch_data *" htab );
48 allow the caller to create and manage a hash search table
49 containing entries consisting of a key (a string) and associated data.
50 Using these functions, only one hash table can be used at a time.
56 are reentrant versions that allow a program to use
57 more than one hash search table at the same time.
60 points to a structure that describes the table
61 on which the function is to operate.
62 The programmer should treat this structure as opaque
63 (i.e., do not attempt to directly access or modify
64 the fields in this structure).
66 First a hash table must be created using
68 The argument \fInel\fP specifies the maximum number of entries
70 (This maximum cannot be changed later, so choose it wisely.)
71 The implementation may adjust this value upward to improve the
72 performance of the resulting hash table.
73 .\" e.g., in glibc it is raised to the next higher prime number
77 function performs the same task as
79 but for the table described by the structure
81 The structure pointed to by
83 must be zeroed before the first call to
88 frees the memory occupied by the hash table that was created by
92 a new hash table can be created using
96 function performs the analogous task for a hash table described by
98 which was previously created using
103 function searches the hash table for an
104 item with the same key as \fIitem\fP (where "the same" is determined using
106 and if successful returns a pointer to it.
108 The argument \fIitem\fP is of type \fIENTRY\fP, which is defined in
109 \fI<search.h>\fP as follows:
113 typedef struct entry {
120 The field \fIkey\fP points to a null-terminated string which is the
122 The field \fIdata\fP points to data that is associated with that key.
124 The argument \fIaction\fP determines what
126 does after an unsuccessful search.
127 This argument must either have the value
129 meaning insert a copy of
131 (and return a pointer to the new hash table entry as the function result),
134 meaning that NULL should be returned.
147 but operates on the hash table described by
151 function differs from
153 in that a pointer to the found item is returned in
155 rather than as the function result.
160 return nonzero on success.
161 They return 0 on error, with
163 set to indicate the error.
167 returns a pointer to an entry in the hash table.
169 returns NULL on error, that is,
170 if \fIaction\fP is \fBENTER\fP and
171 the hash table is full, or \fIaction\fP is \fBFIND\fP and \fIitem\fP
172 cannot be found in the hash table.
174 returns nonzero on success, and 0 on error.
175 In the event of an error, these two functions set
177 to indicate the error.
182 can fail for the following reasons:
191 can fail for the following reasons:
198 was not found in the table,
199 and there was no room in the table to add a new entry.
207 was not found in the table.
209 POSIX.1 specifies only the
210 .\" PROX.1-2001, POSIX.1-2008
214 For an explanation of the terms used in this section, see
220 Interface Attribute Value
227 T} Thread safety MT-Unsafe race:hsearch
234 T} Thread safety MT-Safe race:htab
267 Hash table implementations are usually more efficient when the
268 table contains enough free space to minimize collisions.
269 Typically, this means that
271 should be at least 25% larger than the maximum number of elements
272 that the caller expects to store in the table.
278 functions do not free the buffers pointed to by the
282 elements of the hash table entries.
283 (It can't do this because it doesn't know
284 whether these buffers were allocated dynamically.)
285 If these buffers need to be freed (perhaps because the program
286 is repeatedly creating and destroying hash tables,
287 rather than creating a single table whose lifetime
288 matches that of the program),
289 then the program must maintain bookkeeping data structures that
290 allow it to free them.
292 SVr4 and POSIX.1-2001 specify that \fIaction\fP
293 is significant only for unsuccessful searches, so that an \fBENTER\fP
294 should not do anything for a successful search.
295 In libc and glibc (before glibc 2.3), the
296 implementation violates the specification,
297 updating the \fIdata\fP for the given \fIkey\fP in this case.
299 Individual hash table entries can be added, but not deleted.
301 The following program inserts 24 items into a hash table, then prints
304 .\" SRC BEGIN (hsearch.c)
310 static char *data[] = { "alpha", "bravo", "charlie", "delta",
311 "echo", "foxtrot", "golf", "hotel", "india", "juliet",
312 "kilo", "lima", "mike", "november", "oscar", "papa",
313 "quebec", "romeo", "sierra", "tango", "uniform",
314 "victor", "whisky", "x\-ray", "yankee", "zulu"
325 for (size_t i = 0; i < 24; i++) {
327 /* data is just an integer, instead of a
328 pointer to something */
330 ep = hsearch(e, ENTER);
331 /* there should be no failures */
333 fprintf(stderr, "entry failed\en");
338 for (size_t i = 22; i < 26; i++) {
339 /* print two entries from the table, and
340 show that two are not in the table */
342 ep = hsearch(e, FIND);
343 printf("%9.9s \-> %9.9s:%d\en", e.key,
344 ep ? ep\->key : "NULL", ep ? (int)(ep\->data) : 0);