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
11

NAME

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

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

EXAMPLES

70       The following example reads in strings  followed  by  two  numbers  and
71       stores  them  in  a hash table, discarding duplicates. It then reads in
72       strings and finds the matching entry in the hash table  and  prints  it
73       out.
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‐2008, <search.h>
143
145       Portions of this text are reprinted and reproduced in  electronic  form
146       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
147       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
148       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
149       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
150       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
151       event of any discrepancy between this version and the original IEEE and
152       The  Open Group Standard, the original IEEE and The Open Group Standard
153       is the referee document. The original Standard can be  obtained  online
154       at http://www.unix.org/online.html .
155
156       Any  typographical  or  formatting  errors that appear in this page are
157       most likely to have been introduced during the conversion of the source
158       files  to  man page format. To report such errors, see https://www.ker
159       nel.org/doc/man-pages/reporting_bugs.html .
160
161
162
163IEEE/The Open Group                  2013                          HCREATE(3P)
Impressum