HCREATE(3P)
π HCREATE(3P) β bestie edition π
okay so before we even get into it: this is HCREATE(3P) from the POSIX Programmer's Manual, and it's giving very much "the standard says this, your Linux box might say something else." π
β¨ PROLOG β¨
So like, this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so if you're on Linux, go check the corresponding Linux manual page for the actual tea β β or honestly the interface might not even be implemented on Linux at all. No cap. π
πΈ NAME πΈ
hcreate, hdestroy, hsearch β manage hash search table. that's it, that's the vibe, three little functions running the whole hash table situation π«Ά
π SYNOPSIS π
#include <search.h>
int hcreate(size_t nel);
void hdestroy(void);
ENTRY *hsearch(ENTRY item, ACTION action);
look at them. iconic. π
π¦ DESCRIPTION π¦
okay so hcreate(), hdestroy(), and hsearch() shall manage hash search tables. group project, but make it POSIX.
hcreate() shall allocate sufficient space for the table, and β bestie, listen β the application shall ensure it is called before hsearch() is used. like you literally cannot skip this step, the table has to exist first, that's the whole thing. π
The nel argument is an estimate of the maximum number of entries that the table shall contain. estimate, okay? not a promise. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances. so it might be like "okay girl, we're going bigger" and that's on the algorithm, not you. β¨
hdestroy() shall dispose of the search table, and may be followed by another call to hcreate(). so it's not forever, you can redo the whole thing. BUT β after the call to hdestroy(), the data can no longer be considered accessible. gone. poof. π don't go poking at it.
hsearch() 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 <search.h> header) containing two pointers:
item.keypoints to the comparison key (achar *)item.data(avoid *) points to any other data to be associated with that key
The comparison function used by hsearch() is strcmp(). so like, string compare, that's the whole matching situation π
The action argument is a member of an enumeration type ACTION indicating the disposition of the entry if it cannot be found in the table:
ENTERindicates that the item should be inserted in the table at an appropriate point. β¨FINDindicates that no entry should be made. π
Unsuccessful resolution is indicated by the return of a null pointer. so NULL is the "nope, nothing, sorry bestie" signal.
β οΈ These functions need not be thread-safe. so like, don't be out here sharing them across threads expecting everything to be chill, because it is not. π«
π RETURN VALUE π
hcreate() shall return 0 if it cannot allocate sufficient space for the table; otherwise, it shall return non-zero. zero = sad girl hours π, non-zero = we're so back π₯
hdestroy() shall not return a value. it just... does its thing. no receipts. π
hsearch() shall return a null pointer if either:
- the action is
FINDand the item could not be found, or - the action is
ENTERand the table is full.
either way, null pointer. that's the answer. π
π ERRORS π
The hcreate() and hsearch() functions may fail if:
ENOMEMβ Insufficient storage space is available. like the memory said no. π
The following sections are informative.
π EXAMPLES π
okay this example is genuinely so cute, it reads in strings followed by two numbers, stores them in a hash table, discards duplicates, then reads in strings, finds the matching entry in the hash table and prints it out. obsessed. π§
#include <stdio.h>
#include <search.h>
#include <string.h>
struct info { /* This is the info stored in the table */
int age, room; /* other than the key. */
};
#define NUM_EMPL 5000 /* # of elements in search table. */
int main(void)
{
char string_space[NUM_EMPL*20]; /* Space to store strings. */
struct info info_space[NUM_EMPL]; /* Space to store employee info. */
char *str_ptr = string_space; /* Next space in string_space. */
struct info *info_ptr = info_space;
/* Next space in info_space. */
ENTRY item;
ENTRY *found_item; /* Name to look for in table. */
char name_to_find[30];
int i = 0;
/* Create table; no error checking is performed. */
(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. */
item.key = str_ptr;
item.data = info_ptr;
str_ptr += strlen(str_ptr) + 1;
info_ptr++;
/* Put item into table. */
(void) hsearch(item, ENTER);
}
/* Access table. */
item.key = name_to_find;
while (scanf("%s", item.key) != EOF) {
if ((found_item = hsearch(item, FIND)) != NULL) {
/* If item is in the table. */
(void)printf("found %s, age = %d, room = %d\n",
found_item->key,
((struct info *)found_item->data)->age,
((struct info *)found_item->data)->room);
} else
(void)printf("no such employee %s\n", name_to_find);
}
return 0;
}
π APPLICATION USAGE π
The hcreate() and hsearch() functions may use malloc() to allocate space. so like, that's where the memory is coming from, just so you know. π
β RATIONALE β
None.
(genuinely, that's what it says. the rationale is nothing. silence. we love a minimal queen. π)
π¦ FUTURE DIRECTIONS π¦
None.
(same energy. nothing's changing. periodt. π )
π₯ SEE ALSO π₯
bsearch()lsearch()malloc()strcmp()tdelete()
The Base Definitions volume of POSIX.1β2017, <search.h> π
π 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 .
and that's the whole page bestie, mwah π β this one came from IEEE/The Open Group, 2017, and it was served up fresh from the POSIX Programmer's Manual. go forth and hash responsibly π«Άβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms