hcreate,  hdestroy,  hsearch,  hcreate_r,  hdestroy_r, hsearch_r - hash
   table management


   #include <search.h>

   int hcreate(size_t nel);

   ENTRY *hsearch(ENTRY item, ACTION action);

   void hdestroy(void);

   #define _GNU_SOURCE         /* See feature_test_macros(7) */
   #include <search.h>

   int hcreate_r(size_t nel, struct hsearch_data *htab);

   int hsearch_r(ENTRY item, ACTION action, ENTRY **retval,
                 struct hsearch_data *htab);

   void hdestroy_r(struct hsearch_data *htab);


   The three functions hcreate(),  hsearch(),  and  hdestroy()  allow  the
   caller  to  create  and  manage  a hash search table containing entries
   consisting of a key  (a  string)  and  associated  data.   Using  these
   functions, only one hash table can be used at a time.

   The   three   functions   hcreate_r(),  hsearch_r(),  hdestroy_r()  are
   reentrant versions that allow a program  to  use  more  than  one  hash
   search  table  at  the same time.  The last argument, htab, points to a
   structure that describes the table on which the function is to operate.
   The  programmer  should  treat  this  structure as opaque (i.e., do not
   attempt to directly access or modify the fields in this structure).

   First a hash table must be created using hcreate().  The  argument  nel
   specifies  the  maximum  number of entries in the table.  (This maximum
   cannot be changed later, so choose it wisely.)  The implementation  may
   adjust  this  value  upward to improve the performance of the resulting
   hash table.

   The hcreate_r() function performs the same task as hcreate(),  but  for
   the  table  described by the structure *htab.  The structure pointed to
   by htab must be zeroed before the first call to hcreate_r().

   The function hdestroy() frees the memory occupied  by  the  hash  table
   that  was  created  by  hcreate().  After calling hdestroy() a new hash
   table can  be  created  using  hcreate().   The  hdestroy_r()  function
   performs  the analogous task for a hash table described by *htab, which
   was previously created using hcreate_r().

   The hsearch() function searches the hash table for  an  item  with  the
   same  key as item (where "the same" is determined using strcmp(3)), and
   if successful returns a pointer to it.

   The argument item is of type ENTRY, which is defined in  <search.h>  as

       typedef struct entry {
           char *key;
           void *data;
       } ENTRY;

   The  field  key  points to a null-terminated string which is the search
   key.  The field data points to data that is associated with that key.

   The  argument  action  determines  what   hsearch()   does   after   an
   unsuccessful  search.   This argument must either have the value ENTER,
   meaning insert a copy of item (and return a pointer  to  the  new  hash
   table  entry  as  the function result), or the value FIND, meaning that
   NULL should be returned.  (If action is FIND, then data is ignored.)

   The hsearch_r() function is like hsearch() but  operates  on  the  hash
   table  described  by  *htab.   The  hsearch_r()  function  differs from
   hsearch() in that a pointer to the found item is returned  in  *retval,
   rather than as the function result.


   hcreate()  and hcreate_r() return nonzero on success.  They return 0 on
   error, with errno set to indicate the cause of the error.

   On success, hsearch() returns a pointer to an entry in the hash  table.
   hsearch()  returns  NULL  on error, that is, if action is ENTER and the
   hash table is full, or action is FIND and item cannot be found  in  the
   hash  table.   hsearch_r()  returns nonzero on success, and 0 on error.
   In the event of an error, these two functions set errno to indicate the
   cause of the error.


   hcreate_r() and hdestroy_r() can fail for the following reasons:

   EINVAL htab is NULL.

   hsearch() and hsearch_r() can fail for the following reasons:

   ENOMEM action  was ENTER, key was not found in the table, and there was
          no room in the table to add a new entry.

   ESRCH  action was FIND, and key was not found in the table.

   POSIX.1 specifies only the ENOMEM error.


   For  an  explanation  of  the  terms  used   in   this   section,   see

   │InterfaceAttributeValue                  │
   │hcreate(), hsearch(),     │ Thread safety │ MT-Unsafe race:hsearch │
   │hdestroy()                │               │                        │
   │hcreate_r(), hsearch_r(), │ Thread safety │ MT-Safe race:htab      │
   │hdestroy_r()              │               │                        │


   The  functions  hcreate(), hsearch(), and hdestroy() are from SVr4, and
   are described in POSIX.1-2001 and POSIX.1-2008.

   The  functions  hcreate_r(),  hsearch_r(),  and  hdestroy_r()  are  GNU


   Hash  table  implementations  are usually more efficient when the table
   contains enough free space to  minimize  collisions.   Typically,  this
   means that nel should be at least 25% larger than the maximum number of
   elements that the caller expects to store in the table.

   The hdestroy() and hdestroy_r()  functions  do  not  free  the  buffers
   pointed to by the key and data elements of the hash table entries.  (It
   can't do this because  it  doesn't  know  whether  these  buffers  were
   allocated  dynamically.)   If  these  buffers need to be freed (perhaps
   because the program is repeatedly creating and destroying hash  tables,
   rather  than creating a single table whose lifetime matches that of the
   program), then the program must maintain  bookkeeping  data  structures
   that allow it to free them.


   SVr4  and  POSIX.1-2001  specify  that  action  is significant only for
   unsuccessful searches, so that an ENTER should not do  anything  for  a
   successful  search.   In  libc  and  glibc  (before  version  2.3), the
   implementation violates the specification, updating the  data  for  the
   given key in this case.

   Individual hash table entries can be added, but not deleted.


   The  following  program inserts 24 items into a hash table, then prints
   some of them.

   #include <stdio.h>
   #include <stdlib.h>
   #include <search.h>

   static char *data[] = { "alpha", "bravo", "charlie", "delta",
        "echo", "foxtrot", "golf", "hotel", "india", "juliet",
        "kilo", "lima", "mike", "november", "oscar", "papa",
        "quebec", "romeo", "sierra", "tango", "uniform",
        "victor", "whisky", "x-ray", "yankee", "zulu"

       ENTRY e, *ep;
       int i;


       for (i = 0; i < 24; i++) {
           e.key = data[i];
           /* data is just an integer, instead of a
              pointer to something */
           e.data = (void *) i;
           ep = hsearch(e, ENTER);
           /* there should be no failures */
           if (ep == NULL) {
               fprintf(stderr, "entry failed\n");

       for (i = 22; i < 26; i++) {
           /* print two entries from the table, and
              show that two are not in the table */
           e.key = data[i];
           ep = hsearch(e, FIND);
           printf("%9.9s -> %9.9s:%d\n", e.key,
                  ep ? ep->key : "NULL", ep ? (int)(ep->data) : 0);


   bsearch(3), lsearch(3), malloc(3), tsearch(3)


   This page is part of release 4.09 of the Linux  man-pages  project.   A
   description  of  the project, information about reporting bugs, and the
   latest    version    of    this    page,    can     be     found     at

More Linux Commands

vmcore-dmesg(8) This is just a placeholder until real man pa
vmcore-dmesg extracts the dmesg from a vmcore and write it to standard out. vmcore-dmesg works against either /proc/vmcore in a crash dump capture context or a

ber_start_set(3) - OpenLDAP LBER simplified Basic Encoding R
These routines provide a subroutine interface to a simplified implementation of the Basic Encoding Rules of ASN.1. The version of BER these routines support is

glDrawPixels(3gl) - write a block of pixels to the frame buf
glDrawPixels reads pixel data from memory and writes it into the frame buffer relative to the current raster position, provided that the raster position is vali

XRRSetScreenConfig(3) - X Resize, Rotate and Reflection exte
Xrandr is a simple library designed to interface the X Resize and Rotate Extension. This allows clients to change the size and rotation of the root window of a

smbtar(1) - shell script for backing up SMB/CIFS shares dire
This tool is part of the samba(7) suite. smbtar is a very small shell script on top of smbclient(1) which dumps SMB shares directly to tape. OPTIONS -s server T

XGetIconName(3) - set or read a window's WM_ICON_NAME proper
The XSetWMIconName convenience function calls XSetTextProperty to set the WM_ICON_NAME property. The XGetWMIconName convenience function calls XGetTextProperty

TclXInit(3) - Extended Tcl initialization. ' (Man Page).....
These functions are used to initialize Extended Tcl and applications based on Extended Tcl. This manual page also discusses various issues and approaches of int

ppmtoleaf(1) - convert PPM image to Interleaf image format
This program is part of Netpbm(1) ppmtoleaf reads an Interleaf image file as input and generates a PPM image as output. Interleaf is a now-defunct (actually pur

XtAppErrorMsg(3) - high-level error handlers (Man Page).....
The XtAppErrorMsg function calls the high-level error handler and passes the specified information. The XtAppSetErrorMsgHandler function registers the specified

pcre_compile2(3) - Perl-compatible regular expressions......
This function compiles a regular expression into an internal form. It is the same as pcre[16|32]_compile(), except for the addition of the errorcodeptr argument

ssh-keyconverter(1) - convert ssh v1 keys and authorization
ssh-keyconvert converts RSA public and private keys used for public key based user authentication with protocol version 1 to the format used with protocol versi

gnutls_pubkey_import_rsa_raw(3) - API function (Man Page)...
gnutls_pubkey_import_rsa_raw.3 - This function will replace the parameters in the given structure. The new parameters should be stored in the appropriate gnutls

We can't live, work or learn in freedom unless the software we use is free.