HCREATE(3POSIX)
π hcreate(3POSIX) β the POSIX Programmer's Manual, bestie edition π
okay so real quick, this page lives in the POSIX Programmer's Manual (yes, the 3POSIX one, we're fancy). Heads up tho: the Linux implementation of this interface may differ β so if you're on Linux, go read the corresponding Linux manual page for the actual Linux tea. OR the interface might not even be implemented on Linux at all. π
β¨ 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 <search.h>
int hcreate(size_t nel);
void hdestroy(void);
ENTRY *hsearch(ENTRY item, ACTION action);
πΈ DESCRIPTION πΈ
So the girlies hcreate(), hdestroy(), and hsearch() manage hash search tables. That's the whole job. π
hcreate() allocates sufficient space for the table, and the application shall ensure it is called before hsearch() is used β like, do not skip this step, bestie. The nel argument is an estimate of the maximum number of entries the table shall contain. Key word: estimate. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances. β¨ (the algorithm is doing its little math rituals, let it cook)
hdestroy() disposes 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. gone. poof. π so save what you need first, okay?
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.key points to the comparison key (a char *), and item.data (a void *) points to any other data to be associated with that key. The comparison function used by hsearch() is strcmp(). 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. ENTER indicates that the item should be inserted in the table at an appropriate point. FIND indicates that no entry should be made. Unsuccessful resolution is indicated by the return of a null pointer. π«Ά
ngl these functions need not be thread-safe. so don't be out here sharing them across threads expecting a cute little cooperative moment. π
β RETURN VALUE β
hcreate()shall return0if it cannot allocate sufficient space for the table; otherwise, it shall return non-zero. (yes girl, zero means bad here, iconic chaotic energy)hdestroy()shall not return a value. she simply does not participate in that discourse. πhsearch()shall return a null pointer if either theactionisFINDand the item could not be found, or theactionisENTERand the table is full.
π ERRORS π
The hcreate() and hsearch() functions may fail if:
ENOMEMβ Insufficient storage space is available. π
The following sections are informative. (translation: the drama is over, this is bonus content bestie)
π EXAMPLES π
okay this example is SO good, it reads in strings followed by two numbers and stores them in a hash table, discarding duplicates. Then it reads in strings and finds the matching entry in the hash table and prints it out. serving cunt, honestly. π₯
#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 a thing that may be happening behind the curtain. β¨
π RATIONALE π
None. (the rationale girlies said nothing, and honestly? respect. sometimes silence is the loudest take. π )
π FUTURE DIRECTIONS π
None.
π 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 absolute icon was served to you by IEEE/The Open Group, 2017 β the HCREATE(3POSIX) girlies ate and left no crumbs. go forth and hash responsibly π
β¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms