
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" type="text/css" href="../../../style.css" title="style" />
    <link rel="stylesheet" type="text/css" href="../style.css" title="style" />
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />

    <title>hsearch(3) - Linux manual page</title>
</head>

<body>

<div class="page-top"><a id="top_of_page"></a></div>
<!--%%%TOP_BAR%%%-->
    <div class="nav-bar">
        <table class="nav-table">
            <tr>
                <td class="nav-cell">
                    <p class="nav-text">
                        <a href="http://man7.org/index.html">man7.org</a> &gt; Linux &gt; <a href="../index.html">man-pages</a>
                    </p>
                </td>
                <td class="training-cell">
                    <p class="training-text"><a class="training-link" href="http://man7.org/training/">Linux/UNIX system programming training</a></p>
                </td>
            </tr>
        </table>
    </div>

<hr class="nav-end" />

<!--%%%PAGE_START%%%-->


<table class="sec-table">
<tr>
    <td>
        <p class="section-dir">
<a href="hdestroy.3.html#NAME">NAME</a> | <a href="hdestroy.3.html#SYNOPSIS">SYNOPSIS</a> | <a href="hdestroy.3.html#DESCRIPTION">DESCRIPTION</a> | <a href="hdestroy.3.html#RETURN_VALUE">RETURN&nbsp;VALUE</a> | <a href="hdestroy.3.html#ERRORS">ERRORS</a> | <a href="hdestroy.3.html#ATTRIBUTES">ATTRIBUTES</a> | <a href="hdestroy.3.html#CONFORMING_TO">CONFORMING&nbsp;TO</a> | <a href="hdestroy.3.html#NOTES">NOTES</a> | <a href="hdestroy.3.html#BUGS">BUGS</a> | <a href="hdestroy.3.html#EXAMPLE">EXAMPLE</a> | <a href="hdestroy.3.html#SEE_ALSO">SEE&nbsp;ALSO</a> | <a href="hdestroy.3.html#COLOPHON">COLOPHON</a>
        </p>
    </td>
    <td class="search-box">
        <div class="man-search-box">

            <form method="get" action="http://www.google.com/search">
                <fieldset class="man-search">
                    <input type="text" name="q" size="10" maxlength="255" value="" />
                    <input type="hidden" name="sitesearch" value="man7.org/linux/man-pages" />
                    <input type="submit" name="sa" value="Search online pages" />
                </fieldset>
            </form>

        </div>
    </td>
    <td> </td>
</tr>
</table>

<pre>
<span class="headline">HSEARCH(3)                Linux Programmer's Manual               HSEARCH(3)</span>
</pre>
<h2><a id="NAME" href="hdestroy.3.html#NAME"></a>NAME  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       hcreate,  hdestroy,  hsearch, hcreate_r, hdestroy_r, hsearch_r - hash
       table management
</pre>
<h2><a id="SYNOPSIS" href="hdestroy.3.html#SYNOPSIS"></a>SYNOPSIS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <b>#include &lt;search.h&gt;</b>

       <b>int hcreate(size_t </b><i>nel</i><b>);</b>

       <b>ENTRY *hsearch(ENTRY </b><i>item</i><b>, ACTION </b><i>action</i><b>);</b>

       <b>void hdestroy(void);</b>

       <b>#define _GNU_SOURCE         </b>/* See feature_test_macros(7) */
       <b>#include &lt;search.h&gt;</b>

       <b>int hcreate_r(size_t </b><i>nel</i><b>, struct hsearch_data *</b><i>htab</i><b>);</b>

       <b>int hsearch_r(ENTRY </b><i>item</i><b>, ACTION </b><i>action</i><b>, ENTRY **</b><i>retval</i><b>,</b>
                     <b>struct hsearch_data *</b><i>htab</i><b>);</b>

       <b>void hdestroy_r(struct hsearch_data *</b><i>htab</i><b>);</b>
</pre>
<h2><a id="DESCRIPTION" href="hdestroy.3.html#DESCRIPTION"></a>DESCRIPTION  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The three functions <b>hcreate</b>(), <b>hsearch</b>(), and <b>hdestroy</b>() 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 <b>hcreate_r</b>(), <b>hsearch_r</b>(), <b>hdestroy_r</b>() are
       reentrant versions that allow a program to use more than one hash
       search table at the same time.  The last argument, <i>htab</i>, 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 <b>hcreate</b>().  The argument <i>nel</i>
       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 <b>hcreate_r</b>() function performs the same task as <b>hcreate</b>(), but for
       the table described by the structure <i>*htab</i>.  The structure pointed to
       by <i>htab</i> must be zeroed before the first call to <b>hcreate_r</b>().

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

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

       The argument <i>item</i> is of type <i>ENTRY</i>, which is defined in <i>&lt;search.h&gt;</i> as
       follows:

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

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

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

       The <b>hsearch_r</b>() function is like <b>hsearch</b>() but operates on the hash
       table described by <i>*htab</i>.  The <b>hsearch_r</b>() function differs from
       <b>hsearch</b>() in that a pointer to the found item is returned in <i>*retval</i>,
       rather than as the function result.
</pre>
<h2><a id="RETURN_VALUE" href="hdestroy.3.html#RETURN_VALUE"></a>RETURN VALUE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <b>hcreate</b>() and <b>hcreate_r</b>() return nonzero on success.  They return 0
       on error, with <i><a href="errno.3.html">errno</a></i> set to indicate the cause of the error.

       On success, <b>hsearch</b>() returns a pointer to an entry in the hash
       table.  <b>hsearch</b>() returns NULL on error, that is, if <i>action</i> is <b>ENTER</b>
       and the hash table is full, or <i>action</i> is <b>FIND </b>and <i>item</i> cannot be
       found in the hash table.  <b>hsearch_r</b>() returns nonzero on success, and
       0 on error.  In the event of an error, these two functions set <i><a href="errno.3.html">errno</a></i>
       to indicate the cause of the error.
</pre>
<h2><a id="ERRORS" href="hdestroy.3.html#ERRORS"></a>ERRORS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <b>hcreate_r</b>() and <b>hdestroy_r</b>() can fail for the following reasons:

       <b>EINVAL </b><i>htab</i> is NULL.

       <b>hsearch</b>() and <b>hsearch_r</b>() can fail for the following reasons:

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

       <b>ESRCH  </b><i>action</i> was <b>FIND</b>, and <i>key</i> was not found in the table.

       POSIX.1 specifies only the <b>ENOMEM </b>error.
</pre>
<h2><a id="ATTRIBUTES" href="hdestroy.3.html#ATTRIBUTES"></a>ATTRIBUTES  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       For an explanation of the terms used in this section, see
       <a href="../man7/attributes.7.html">attributes(7)</a>.

       ┌──────────────────────────┬───────────────┬────────────────────────┐
       │<b>Interface                 </b>│ <b>Attribute     </b>│ <b>Value                  </b>│
       ├──────────────────────────┼───────────────┼────────────────────────┤
       │<b>hcreate</b>(), <b>hsearch</b>(),     │ Thread safety │ MT-Unsafe race:hsearch │
       │<b>hdestroy</b>()                │               │                        │
       ├──────────────────────────┼───────────────┼────────────────────────┤
       │<b>hcreate_r</b>(), <b>hsearch_r</b>(), │ Thread safety │ MT-Safe race:htab      │
       │<b>hdestroy_r</b>()              │               │                        │
       └──────────────────────────┴───────────────┴────────────────────────┘
</pre>
<h2><a id="CONFORMING_TO" href="hdestroy.3.html#CONFORMING_TO"></a>CONFORMING TO  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The functions <b>hcreate</b>(), <b>hsearch</b>(), and <b>hdestroy</b>() are from SVr4, and
       are described in POSIX.1-2001 and POSIX.1-2008.

       The functions <b>hcreate_r</b>(), <b>hsearch_r</b>(), and <b>hdestroy_r</b>() are GNU
       extensions.
</pre>
<h2><a id="NOTES" href="hdestroy.3.html#NOTES"></a>NOTES  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       Hash table implementations are usually more efficient when the table
       contains enough free space to minimize collisions.  Typically, this
       means that <i>nel</i> should be at least 25% larger than the maximum number
       of elements that the caller expects to store in the table.

       The <b>hdestroy</b>() and <b>hdestroy_r</b>() functions do not free the buffers
       pointed to by the <i>key</i> and <i>data</i> 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.
</pre>
<h2><a id="BUGS" href="hdestroy.3.html#BUGS"></a>BUGS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       SVr4 and POSIX.1-2001 specify that <i>action</i> is significant only for
       unsuccessful searches, so that an <b>ENTER </b>should not do anything for a
       successful search.  In libc and glibc (before version 2.3), the
       implementation violates the specification, updating the <i>data</i> for the
       given <i>key</i> in this case.

       Individual hash table entries can be added, but not deleted.
</pre>
<h2><a id="EXAMPLE" href="hdestroy.3.html#EXAMPLE"></a>EXAMPLE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The following program inserts 24 items into a hash table, then prints
       some of them.

       #include &lt;stdio.h&gt;
       #include &lt;stdlib.h&gt;
       #include &lt;search.h&gt;

       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"
       };

       int
       main(void)
       {
           ENTRY e, *ep;
           int i;

           hcreate(30);

           for (i = 0; i &lt; 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");
                   exit(EXIT_FAILURE);
               }
           }

           for (i = 22; i &lt; 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 -&gt; %9.9s:%d\n", e.key,
                      ep ? ep-&gt;key : "NULL", ep ? (int)(ep-&gt;data) : 0);
           }
           hdestroy();
           exit(EXIT_SUCCESS);
       }
</pre>
<h2><a id="SEE_ALSO" href="hdestroy.3.html#SEE_ALSO"></a>SEE ALSO  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <a href="bsearch.3.html">bsearch(3)</a>, <a href="lsearch.3.html">lsearch(3)</a>, <a href="malloc.3.html">malloc(3)</a>, <a href="tsearch.3.html">tsearch(3)</a>
</pre>
<h2><a id="COLOPHON" href="hdestroy.3.html#COLOPHON"></a>COLOPHON  &nbsp; &nbsp; &nbsp; &nbsp; <a href="hdestroy.3.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       This page is part of release 4.15 of the Linux <i>man-pages</i> project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at
       <a href="https://www.kernel.org/doc/man-pages/">https://www.kernel.org/doc/man-pages/</a>.

<span class="footline">GNU                              2017-09-15                       HSEARCH(3)</span>
</pre>

<hr class="end-man-text" />
<p>Pages that refer to this page: 
    <a href="bsearch.3.html">bsearch(3)</a>,&nbsp; 
    <a href="lsearch.3.html">lsearch(3)</a>,&nbsp; 
    <a href="tsearch.3.html">tsearch(3)</a>
</p>
<hr/>

<p class="page-copyright"><a href="hsearch.3.license.html">Copyright and license for this manual page</a></p> 
<hr class="start-footer" />

<div class="footer"> 

<table class="colophon-table">
    <tr>
    <td class="pub-info">
        <p>
            HTML rendering created 2018-02-02
            by <a href="http://man7.org/mtk/index.html">Michael Kerrisk</a>, 
            author of 
            <a href="http://man7.org/tlpi/"><em>The Linux Programming Interface</em></a>, 
            maintainer of the 
            <a href="https://www.kernel.org/doc/man-pages/">Linux <em>man-pages</em> project</a>.
        </p>
        <p>
            For details of in-depth
            <strong>Linux/UNIX system programming training courses</strong>
            that I teach, look <a href="http://man7.org/training/">here</a>.
        </p>
        <p>
            Hosting by <a href="http://www.jambit.com/index_en.html">jambit GmbH</a>.
        </p>
        <p>
            <a href="http://validator.w3.org/check?uri=referer">
            <img src="http://www.w3.org/Icons/valid-xhtml11"
                alt="Valid XHTML 1.1" height="31" width="88" />
            </a>
        </p>
    </td>
    <td class="colophon-divider">
    </td>
    <td class="tlpi-cover">
        <a href="http://man7.org/tlpi/"><img src="../../../tlpi/cover/TLPI-front-cover-vsmall.png" alt="Cover of TLPI" /></a>
    </td>
    </tr>
</table>

</div>

<hr class="end-footer" />



<!--BEGIN-SITETRACKING-->
<!-- SITETRACKING.man7.org_linux_man-pages -->

<!-- Start of StatCounter Code (xhtml) -->

<script type="text/javascript">
//<![CDATA[
var sc_project=7422636; 
var sc_invisible=1; 
var sc_security="9b6714ff"; 
//]]>
</script>
<script type="text/javascript"
src="http://www.statcounter.com/counter/counter_xhtml.js"></script>
<noscript><div class="statcounter"><a title="website
statistics" href="http://statcounter.com/"
class="statcounter"><img class="statcounter"
src="http://c.statcounter.com/7422636/0/9b6714ff/1/"
alt="website statistics" /></a></div></noscript>

<!-- End of StatCounter Code -->


<!-- Start of Google Analytics Code -->

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-9830363-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

<!-- End of Google Analytics Code -->

<!--END-SITETRACKING-->

</body>
</html>
