1HCREATE(3P)                POSIX Programmer's Manual               HCREATE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       hcreate, hdestroy, hsearch — manage hash search table
13

SYNOPSIS

15       #include <search.h>
16
17       int hcreate(size_t nel);
18       void hdestroy(void);
19       ENTRY *hsearch(ENTRY item, ACTION action);
20

DESCRIPTION

22       The hcreate(), hdestroy(), and hsearch() functions  shall  manage  hash
23       search tables.
24
25       The  hcreate()  function shall allocate sufficient space for the table,
26       and the application shall ensure it is called before hsearch() is used.
27       The  nel  argument is an estimate of the maximum number of entries that
28       the table shall contain. This number may  be  adjusted  upward  by  the
29       algorithm  in  order to obtain certain mathematically favorable circum‐
30       stances.
31
32       The hdestroy() function shall dispose of the search table, and  may  be
33       followed  by  another call to hcreate().  After the call to hdestroy(),
34       the data can no longer be considered accessible.
35
36       The hsearch() function is a hash-table search routine. It shall  return
37       a  pointer  into a hash table indicating the location at which an entry
38       can be found. The item argument is a structure of type  ENTRY  (defined
39       in  the  <search.h> header) containing two pointers: item.key points to
40       the comparison key (a char *), and item.data (a void *) points  to  any
41       other data to be associated with that key. The comparison function used
42       by hsearch() is strcmp().  The action argument is a member of  an  enu‐
43       meration type ACTION indicating the disposition of the entry if it can‐
44       not be found in the table. ENTER indicates  that  the  item  should  be
45       inserted  in  the table at an appropriate point. FIND indicates that no
46       entry should be made.  Unsuccessful  resolution  is  indicated  by  the
47       return of a null pointer.
48
49       These functions need not be thread-safe.
50

RETURN VALUE

52       The  hcreate() function shall return 0 if it cannot allocate sufficient
53       space for the table; otherwise, it shall return non-zero.
54
55       The hdestroy() function shall not return a value.
56
57       The hsearch() function shall return a null pointer if either the action
58       is  FIND and the item could not be found or the action is ENTER and the
59       table is full.
60

ERRORS

62       The hcreate() and hsearch() functions may fail if:
63
64       ENOMEM Insufficient storage space is available.
65
66       The following sections are informative.
67

EXAMPLES

69       The following example reads in strings  followed  by  two  numbers  and
70       stores  them  in  a hash table, discarding duplicates. It then reads in
71       strings and finds the matching entry in the hash table  and  prints  it
72       out.
73
74
75           #include <stdio.h>
76           #include <search.h>
77           #include <string.h>
78
79           struct info {        /* This is the info stored in the table */
80               int age, room;   /* other than the key. */
81           };
82
83           #define NUM_EMPL    5000    /* # of elements in search table. */
84
85           int main(void)
86           {
87               char string_space[NUM_EMPL*20];   /* Space to store strings. */
88               struct info info_space[NUM_EMPL]; /* Space to store employee info. */
89               char *str_ptr = string_space;     /* Next space in string_space. */
90               struct info *info_ptr = info_space;
91                                                 /* Next space in info_space. */
92               ENTRY item;
93               ENTRY *found_item; /* Name to look for in table. */
94               char name_to_find[30];
95
96               int i = 0;
97
98               /* Create table; no error checking is performed. */
99               (void) hcreate(NUM_EMPL);
100               while (scanf("%s%d%d", str_ptr, &info_ptr->age,
101                      &info_ptr->room) != EOF && i++ < NUM_EMPL) {
102
103                   /* Put information in structure, and structure in item. */
104                   item.key = str_ptr;
105                   item.data = info_ptr;
106                   str_ptr += strlen(str_ptr) + 1;
107                   info_ptr++;
108
109                   /* Put item into table. */
110                   (void) hsearch(item, ENTER);
111               }
112
113               /* Access table. */
114               item.key = name_to_find;
115               while (scanf("%s", item.key) != EOF) {
116                   if ((found_item = hsearch(item, FIND)) != NULL) {
117
118                       /* If item is in the table. */
119                       (void)printf("found %s, age = %d, room = %d\n",
120                           found_item->key,
121                           ((struct info *)found_item->data)->age,
122                           ((struct info *)found_item->data)->room);
123                   } else
124                       (void)printf("no such employee %s\n", name_to_find);
125               }
126               return 0;
127           }
128

APPLICATION USAGE

130       The  hcreate()  and  hsearch()  functions  may use malloc() to allocate
131       space.
132

RATIONALE

134       None.
135

FUTURE DIRECTIONS

137       None.
138

SEE ALSO

140       bsearch(), lsearch(), malloc(), strcmp(), tdelete()
141
142       The Base Definitions volume of POSIX.1‐2017, <search.h>
143
145       Portions of this text are reprinted and reproduced in  electronic  form
146       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
147       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
148       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
149       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
150       event of any discrepancy between this version and the original IEEE and
151       The Open Group Standard, the original IEEE and The Open Group  Standard
152       is  the  referee document. The original Standard can be obtained online
153       at http://www.opengroup.org/unix/online.html .
154
155       Any typographical or formatting errors that appear  in  this  page  are
156       most likely to have been introduced during the conversion of the source
157       files to man page format. To report such errors,  see  https://www.ker
158       nel.org/doc/man-pages/reporting_bugs.html .
159
160
161
162IEEE/The Open Group                  2017                          HCREATE(3P)
Impressum