HCREATE
PROLOG
NAME
SYNOPSIS
DESCRIPTION
RETURN VALUE
ERRORS
EXAMPLES
APPLICATION USAGE
RATIONALE
FUTURE DIRECTIONS
SEE ALSO
COPYRIGHT
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
NAME
hcreate, hdestroy, hsearch — manage hash search table
SYNOPSIS
#include
int hcreate(size_t nel);
void hdestroy(void);
ENTRY *hsearch(ENTRY item, ACTION action);
DESCRIPTION
The hcreate(), hdestroy(), and hsearch() functions shall manage hash search tables.
The hcreate() function shall allocate sufficient space for the table, and the application shall ensure it is called before hsearch() is used. The nel argument is an estimate of the maximum number of entries that the table shall contain. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances.
The hdestroy() function shall dispose of the search table, and may be followed by another call to hcreate(). After the call to hdestroy(), the data can no longer be considered accessible.
The hsearch() function is a hash-table search routine. It shall return a pointer into a hash table indicating the location at which an entry can be found. The item argument is a structure of type ENTRY (defined in the
These functions need not be thread-safe.
RETURN VALUE
The hcreate() function shall return 0 if it cannot allocate sufficient space for the table; otherwise, it shall return non-zero.
The hdestroy() function shall not return a value.
The hsearch() function shall return a null pointer if either the action is FIND and the item could not be found or the action is ENTER and the table is full.
ERRORS
The hcreate() and hsearch() functions may fail if:
ENOMEM |
Insufficient storage space is available. |
The following sections are informative.
EXAMPLES
The following example reads in strings followed by two numbers and stores them in a hash table, discarding duplicates. It then reads in strings and finds the matching entry in the hash table and prints it out.
#include
#include
#include
struct info { /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var This is the info stored in the table bodies/ usr/
int age, room; /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var other than the key. bodies/ usr/
};
#define NUM_EMPL 5000 /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var # of elements in search table. */
int main(void)
{
char string_space[NUM_EMPL*20]; /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var Space to store strings. bodies/ usr/
struct info info_space[NUM_EMPL]; /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var Space to store employee info. bodies/ usr/
char *str_ptr = string_space; /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var Next space in string_space. bodies/ usr/
struct info *info_ptr = info_space;
/bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var Next space in info_space. bodies/ usr/
ENTRY item;
ENTRY *found_item; /bin /boot /dead.letter /dev /etc /home /initrd /lib /lib64 /lost+found /media /mnt /opt /proc /release-notes.html /release-notes.txt /root /run /sbin /srv /sys /tmp /usr /var Name to look for in table. bodies/ usr/
char name_to_find[30];
int i = 0;
/* Create table; no error checking is performed. bodies/ usr/
(void) hcreate(NUM_EMPL);
while (scanf(“%s%d%d”, str_ptr, &info_ptr->age,
&info_ptr->room) != EOF && i++ < NUM_EMPL) {
/* Put information in structure, and structure in item. bodies/ usr/
item.key = str_ptr;
item.data = info_ptr;
str_ptr += strlen(str_ptr) + 1;
info_ptr++;
/* Put item into table. bodies/ usr/
(void) hsearch(item, ENTER);
}
/* Access table. bodies/ usr/
item.key = name_to_find;
while (scanf(“%s”, item.key) != EOF) {
if ((found_item = hsearch(item, FIND)) != NULL) {
/* If item is in the table. bodies/ usr/
(void)printf(“found %s, age = %d, room = %dn”,
found_item->key,
((struct info *)found_item->data)->age,
((struct info *)found_item->data)->room);
} else
(void)printf(“no such employee %sn”, name_to_find);
}
return 0;
}
APPLICATION USAGE
The hcreate() and hsearch() functions may use malloc() to allocate space.
RATIONALE
None.
FUTURE DIRECTIONS
None.
SEE ALSO
bsearch(), lsearch(), malloc(), strcmp(), tdelete()
The Base Definitions volume of POSIX.1-2017,
COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology — Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .