Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


hcreate(3p) — Linux manual page

PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT

HCREATE(3P)             POSIX Programmer's ManualHCREATE(3P)

PROLOG        top

       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        top

       hcreate, hdestroy, hsearch — manage hash search table

SYNOPSIS        top

       #include <search.h>       int hcreate(size_tnel);       void hdestroy(void);       ENTRY *hsearch(ENTRYitem, ACTIONaction);

DESCRIPTION        top

       Thehcreate(),hdestroy(), andhsearch() functions shall manage       hash search tables.       Thehcreate() function shall allocate sufficient space for the       table, and the application shall ensure it is called beforehsearch() is used. Thenel 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.       Thehdestroy() function shall dispose of the search table, and may       be followed by another call tohcreate().  After the call tohdestroy(), the data can no longer be considered accessible.       Thehsearch() 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. Theitem argument is a structure of       typeENTRY(defined in the<search.h> header) containing two       pointers:item.key points to the comparison key (achar *), anditem.data (avoid *) points to any other data to be associated       with that key. The comparison function used byhsearch() isstrcmp().  Theaction argument is a member of an enumeration typeACTIONindicating 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.       These functions need not be thread-safe.

RETURN VALUE        top

       Thehcreate() function shall return 0 if it cannot allocate       sufficient space for the table; otherwise, it shall return non-       zero.       Thehdestroy() function shall not return a value.       Thehsearch() 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        top

       Thehcreate() andhsearch() functions may fail if:ENOMEMInsufficient storage space is available.The following sections are informative.

EXAMPLES        top

       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 <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        top

       Thehcreate() andhsearch() functions may usemalloc() to allocate       space.

RATIONALE        top

       None.

FUTURE DIRECTIONS        top

       None.

SEE ALSO        top

bsearch(3p),lsearch(3p),malloc(3p),strcmp(3p),tdelete(3p)       The Base Definitions volume of POSIX.1‐2017,search.h(0p)

COPYRIGHT        top

       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 athttp://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, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group                2017HCREATE(3P)

Pages that refer to this page:search.h(0p)bsearch(3p)lsearch(3p)tdelete(3p)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp