1HSEARCH(3)                 Linux Programmer's Manual                HSEARCH(3)
2
3
4

NAME

6       hcreate, hdestroy, hsearch, hcreate_r, hdestroy_r, hsearch_r - hash ta‐
7       ble management
8

SYNOPSIS

10       #include <search.h>
11
12       int hcreate(size_t nel);
13       void hdestroy(void);
14
15       ENTRY *hsearch(ENTRY item, ACTION action);
16
17       #define _GNU_SOURCE         /* See feature_test_macros(7) */
18       #include <search.h>
19
20       int hcreate_r(size_t nel, struct hsearch_data *htab);
21       void hdestroy_r(struct hsearch_data *htab);
22
23       int hsearch_r(ENTRY item, ACTION action, ENTRY **retval,
24                     struct hsearch_data *htab);
25

DESCRIPTION

27       The three functions hcreate(),  hsearch(),  and  hdestroy()  allow  the
28       caller to create and manage a hash search table containing entries con‐
29       sisting of a key (a string) and associated  data.   Using  these  func‐
30       tions, only one hash table can be used at a time.
31
32       The  three  functions  hcreate_r(), hsearch_r(), hdestroy_r() are reen‐
33       trant versions that allow a program to use more than  one  hash  search
34       table at the same time.  The last argument, htab, points to a structure
35       that describes the table on which the function is to operate.  The pro‐
36       grammer  should treat this structure as opaque (i.e., do not attempt to
37       directly access or modify the fields in this structure).
38
39       First a hash table must be created using hcreate().  The  argument  nel
40       specifies  the  maximum  number of entries in the table.  (This maximum
41       cannot be changed later, so choose it wisely.)  The implementation  may
42       adjust  this  value  upward to improve the performance of the resulting
43       hash table.
44
45       The hcreate_r() function performs the same task as hcreate(),  but  for
46       the  table  described by the structure *htab.  The structure pointed to
47       by htab must be zeroed before the first call to hcreate_r().
48
49       The function hdestroy() frees the memory occupied  by  the  hash  table
50       that  was  created  by hcreate().  After calling hdestroy(), a new hash
51       table can be created using hcreate().  The hdestroy_r()  function  per‐
52       forms the analogous task for a hash table described by *htab, which was
53       previously created using hcreate_r().
54
55       The hsearch() function searches the hash table for  an  item  with  the
56       same  key as item (where "the same" is determined using strcmp(3)), and
57       if successful returns a pointer to it.
58
59       The argument item is of type ENTRY, which is defined in  <search.h>  as
60       follows:
61
62           typedef struct entry {
63               char *key;
64               void *data;
65           } ENTRY;
66
67       The  field  key  points to a null-terminated string which is the search
68       key.  The field data points to data that is associated with that key.
69
70       The argument action determines what hsearch() does after an  unsuccess‐
71       ful  search.   This  argument must either have the value ENTER, meaning
72       insert a copy of item (and return a pointer to the new hash table entry
73       as the function result), or the value FIND, meaning that NULL should be
74       returned.  (If action is FIND, then data is ignored.)
75
76       The hsearch_r() function is like hsearch() but operates on the hash ta‐
77       ble   described  by  *htab.   The  hsearch_r()  function  differs  from
78       hsearch() in that a pointer to the found item is returned  in  *retval,
79       rather than as the function result.
80

RETURN VALUE

82       hcreate()  and hcreate_r() return nonzero on success.  They return 0 on
83       error, with errno set to indicate the error.
84
85       On success, hsearch() returns a pointer to an entry in the hash  table.
86       hsearch()  returns  NULL  on error, that is, if action is ENTER and the
87       hash table is full, or action is FIND and item cannot be found  in  the
88       hash  table.   hsearch_r()  returns nonzero on success, and 0 on error.
89       In the event of an error, these two functions set errno to indicate the
90       error.
91

ERRORS

93       hcreate_r() and hdestroy_r() can fail for the following reasons:
94
95       EINVAL htab is NULL.
96
97       hsearch() and hsearch_r() can fail for the following reasons:
98
99       ENOMEM action  was ENTER, key was not found in the table, and there was
100              no room in the table to add a new entry.
101
102       ESRCH  action was FIND, and key was not found in the table.
103
104       POSIX.1 specifies only the ENOMEM error.
105

ATTRIBUTES

107       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
108       tributes(7).
109
110       ┌─────────────────────────────┬───────────────┬────────────────────────┐
111Interface                    Attribute     Value                  
112       ├─────────────────────────────┼───────────────┼────────────────────────┤
113hcreate(), hsearch(),        │ Thread safety │ MT-Unsafe race:hsearch │
114hdestroy()                   │               │                        │
115       ├─────────────────────────────┼───────────────┼────────────────────────┤
116hcreate_r(), hsearch_r(),    │ Thread safety │ MT-Safe race:htab      │
117hdestroy_r()                 │               │                        │
118       └─────────────────────────────┴───────────────┴────────────────────────┘
119

CONFORMING TO

121       The  functions  hcreate(), hsearch(), and hdestroy() are from SVr4, and
122       are described in POSIX.1-2001 and POSIX.1-2008.
123
124       The functions hcreate_r(), hsearch_r(), and hdestroy_r() are GNU exten‐
125       sions.
126

NOTES

128       Hash  table  implementations  are usually more efficient when the table
129       contains enough free space to  minimize  collisions.   Typically,  this
130       means that nel should be at least 25% larger than the maximum number of
131       elements that the caller expects to store in the table.
132
133       The hdestroy() and hdestroy_r()  functions  do  not  free  the  buffers
134       pointed to by the key and data elements of the hash table entries.  (It
135       can't do this because it doesn't know whether these buffers were  allo‐
136       cated dynamically.)  If these buffers need to be freed (perhaps because
137       the program is repeatedly creating and destroying hash  tables,  rather
138       than  creating  a  single table whose lifetime matches that of the pro‐
139       gram), then the program must maintain bookkeeping data structures  that
140       allow it to free them.
141

BUGS

143       SVr4  and  POSIX.1-2001 specify that action is significant only for un‐
144       successful searches, so that an ENTER should not do anything for a suc‐
145       cessful  search.  In libc and glibc (before version 2.3), the implemen‐
146       tation violates the specification, updating the data for the given  key
147       in this case.
148
149       Individual hash table entries can be added, but not deleted.
150

EXAMPLES

152       The  following  program inserts 24 items into a hash table, then prints
153       some of them.
154
155       #include <stdio.h>
156       #include <stdlib.h>
157       #include <search.h>
158
159       static char *data[] = { "alpha", "bravo", "charlie", "delta",
160            "echo", "foxtrot", "golf", "hotel", "india", "juliet",
161            "kilo", "lima", "mike", "november", "oscar", "papa",
162            "quebec", "romeo", "sierra", "tango", "uniform",
163            "victor", "whisky", "x-ray", "yankee", "zulu"
164       };
165
166       int
167       main(void)
168       {
169           ENTRY e;
170           ENTRY *ep;
171
172           hcreate(30);
173
174           for (int i = 0; i < 24; i++) {
175               e.key = data[i];
176               /* data is just an integer, instead of a
177                  pointer to something */
178               e.data = (void *) i;
179               ep = hsearch(e, ENTER);
180               /* there should be no failures */
181               if (ep == NULL) {
182                   fprintf(stderr, "entry failed\n");
183                   exit(EXIT_FAILURE);
184               }
185           }
186
187           for (int i = 22; i < 26; i++) {
188               /* print two entries from the table, and
189                  show that two are not in the table */
190               e.key = data[i];
191               ep = hsearch(e, FIND);
192               printf("%9.9s -> %9.9s:%d\n", e.key,
193                      ep ? ep->key : "NULL", ep ? (int)(ep->data) : 0);
194           }
195           hdestroy();
196           exit(EXIT_SUCCESS);
197       }
198

SEE ALSO

200       bsearch(3), lsearch(3), malloc(3), tsearch(3)
201

COLOPHON

203       This page is part of release 5.13 of the Linux  man-pages  project.   A
204       description  of  the project, information about reporting bugs, and the
205       latest    version    of    this    page,    can     be     found     at
206       https://www.kernel.org/doc/man-pages/.
207
208
209
210GNU                               2021-03-22                        HSEARCH(3)
Impressum