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