1HSEARCH(3) Linux Programmer's Manual HSEARCH(3)
2
3
4
6 hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r, hsearch_r - hash ta‐
7 ble management
8
10 #include <search.h>
11
12 int hcreate(size_t nel);
13
14 ENTRY *hsearch(ENTRY item, ACTION action);
15
16 void hdestroy(void);
17
18 #define _GNU_SOURCE
19 #include <search.h>
20
21 int hcreate_r(size_t nel, struct hsearch_data *htab);
22
23 int hsearch_r(ENTRY item, ACTION action, ENTRY **retval,
24 struct hsearch_data *htab);
25
26 void hdestroy_r(struct hsearch_data *htab);
27
29 The three functions hcreate(), hsearch(), and hdestroy() allow the
30 caller to create and manage a hash search table containing entries con‐
31 sisting of a key (a string) and associated data. Using these func‐
32 tions, only one hash table can be used at a time.
33
34 The three functions hcreate_r(), hsearch_r(), hdestroy_r() are reen‐
35 trant versions that allow a program to use more than one hash search
36 table at the same time. The last argument, htab, points to a structure
37 that describes the table on which the function is to operate. The pro‐
38 grammer should treat this structure as opaque (i.e., do not attempt to
39 directly access or modify the fields in this structure).
40
41 First a hash table must be created using hcreate(). The argument nel
42 specifies the maximum number of entries in the table. (This maximum
43 cannot be changed later, so choose it wisely.) The implementation may
44 adjust this value upward to improve the performance of the resulting
45 hash table.
46
47 The hcreate_r() function performs the same task as hcreate(), but for
48 the table described by the structure *htab. The structure pointed to
49 by htab must be zeroed before the first call to hcreate_r().
50
51 The function hdestroy() frees the memory occupied by the hash table
52 that was created by hcreate(). After calling hdestroy() a new hash ta‐
53 ble can be created using hcreate(). The hdestroy_r() function performs
54 the analogous task for a hash table described by *htab, which was pre‐
55 viously created using hcreate_r().
56
57 The hsearch() function searches the hash table for an item with the
58 same key as item (where "the same" is determined using strcmp(3)), and
59 if successful returns a pointer to it.
60
61 The argument item is of type ENTRY, which is defined in <search.h> as
62 follows:
63
64 typedef struct entry {
65 char *key;
66 void *data;
67 } ENTRY;
68
69 The field key points to a null-terminated string which is the search
70 key. The field data points to data that is associated with that key.
71
72 The argument action determines what hsearch() does after an unsuccess‐
73 ful search. This argument must either have the value ENTER, meaning
74 insert a copy of item (and return a pointer to the new hash table entry
75 as the function result), or the value FIND, meaning that NULL should be
76 returned. (If action is FIND, then data is ignored.)
77
78 The hsearch_r() function is like hsearch() but operates on the hash ta‐
79 ble described by *htab. The hsearch_r() function differs from
80 hsearch() in that a pointer to the found item is returned in *retval,
81 rather than as the function result.
82
84 hcreate() and hcreate_r() return non-zero on success. They return 0 on
85 error.
86
87 On success, hsearch() returns a pointer to an entry in the hash table.
88 hsearch() returns NULL on error, that is, if action is ENTER and the
89 hash table is full, or action is FIND and item cannot be found in the
90 hash table. hsearch_r() returns non-zero on success, and 0 on error.
91
93 hcreate() and hcreate_r() can fail for the following reasons:
94
95 EINVAL (hcreate_r()) htab is NULL.
96
97 ENOMEM Table full with action set to ENTER.
98
99 ESRCH The action argument is FIND and no corresponding element is
100 found in the table.
101
102 hsearch() and hsearch_r() can fail for the following reasons:
103
104 ENOMEM action was ENTER, key was not found in the table, and there was
105 no room in the table to add a new entry.
106
107 ESRCH action was FIND, and key was not found in the table.
108
109 POSIX.1-2001 only specifies the ENOMEM error.
110
112 The functions hcreate(), hsearch(), and hdestroy() are from SVr4, and
113 are described in POSIX.1-2001. The functions hcreate_r(), hsearch_r(),
114 and hdestroy_r() are GNU extensions.
115
117 Hash table implementations are usually more efficient when the table
118 contains enough free space to minimize collisions. Typically, this
119 means that nel should be at least 25% larger than the maximum number of
120 elements that the caller expects to store in the table.
121
122 The hdestroy() and hdestroy_r() functions do not free the buffers
123 pointed to by the key and data elements of the hash table entries. (It
124 can't do this because it doesn't know whether these buffers were allo‐
125 cated dynamically.) If these buffers need to be freed (perhaps because
126 the program is repeatedly creating and destroying hash tables, rather
127 than creating a single table whose lifetime matches that of the pro‐
128 gram), then the program must maintain bookkeeping data structures that
129 allow it to free them.
130
132 SVr4 and POSIX.1-2001 specify that action is significant only for
133 unsuccessful searches, so that an ENTER should not do anything for a
134 successful search. In libc and glibc (before version 2.3), the imple‐
135 mentation violates the specification, updating the data for the given
136 key in this case.
137
138 Individual hash table entries can be added, but not deleted.
139
141 The following program inserts 24 items into a hash table, then prints
142 some of them.
143
144 #include <stdio.h>
145 #include <stdlib.h>
146 #include <search.h>
147
148 char *data[] = { "alpha", "bravo", "charlie", "delta",
149 "echo", "foxtrot", "golf", "hotel", "india", "juliet",
150 "kilo", "lima", "mike", "november", "oscar", "papa",
151 "quebec", "romeo", "sierra", "tango", "uniform",
152 "victor", "whisky", "x-ray", "yankee", "zulu"
153 };
154
155 int
156 main(void)
157 {
158 ENTRY e, *ep;
159 int i;
160
161 hcreate(30);
162
163 for (i = 0; i < 24; i++) {
164 e.key = data[i];
165 /* data is just an integer, instead of a
166 pointer to something */
167 e.data = (void *) i;
168 ep = hsearch(e, ENTER);
169 /* there should be no failures */
170 if (ep == NULL) {
171 fprintf(stderr, "entry failed\n");
172 exit(EXIT_FAILURE);
173 }
174 }
175
176 for (i = 22; i < 26; i++) {
177 /* print two entries from the table, and
178 show that two are not in the table */
179 e.key = data[i];
180 ep = hsearch(e, FIND);
181 printf("%9.9s -> %9.9s:%d\n", e.key,
182 ep ? ep->key : "NULL", ep ? (int)(ep->data) : 0);
183 }
184 hdestroy();
185 exit(EXIT_SUCCESS);
186 }
187
189 bsearch(3), lsearch(3), malloc(3), tsearch(3), feature_test_macros(7)
190
192 This page is part of release 3.22 of the Linux man-pages project. A
193 description of the project, and information about reporting bugs, can
194 be found at http://www.kernel.org/doc/man-pages/.
195
196
197
198GNU 2008-10-06 HSEARCH(3)