diff options
author | dwmalone <dwmalone@FreeBSD.org> | 2003-03-12 14:18:14 +0000 |
---|---|---|
committer | dwmalone <dwmalone@FreeBSD.org> | 2003-03-12 14:18:14 +0000 |
commit | 5b336b4502a0fd03fc0092e117c4410055503696 (patch) | |
tree | 0e257cba47f1ab253d1e19d97f2b9f4a91a6074b | |
parent | 0580d0b0d9793bd5a0a3cee00fddf493ca362892 (diff) | |
download | FreeBSD-src-5b336b4502a0fd03fc0092e117c4410055503696.zip FreeBSD-src-5b336b4502a0fd03fc0092e117c4410055503696.tar.gz |
Document the fact that hdestory calls free on the keys added with
hsearch(.., ENTER). Make the example reflect this.
PR: 49951
Submitted by: Peter Jeremy <peterjeremy@optushome.com.au>
-rw-r--r-- | lib/libc/stdlib/hcreate.3 | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/lib/libc/stdlib/hcreate.3 b/lib/libc/stdlib/hcreate.3 index 0894789..df46b2d 100644 --- a/lib/libc/stdlib/hcreate.3 +++ b/lib/libc/stdlib/hcreate.3 @@ -44,6 +44,12 @@ function disposes of the search table, and may be followed by another call to After the call to .Fn hdestroy , the data can no longer be considered accessible. +The +.Fn hdestroy +function calls +.Xr free 3 +for each comparison key in the search table +but not the data item associated with the key. .Pp The .Fn hsearch @@ -88,6 +94,20 @@ Unsuccessful resolution is indicated by the return of a .Dv NULL pointer. +.Pp +The comparison key (passed to +.Fn hsearch +as +.Fa item.key ) +must be allocated using +.Xr malloc 3 +if +.Fa action +is +.Dv ENTER +and +.Fn hdestroy +is called. .Sh RETURN VALUES The .Fn hcreate @@ -132,6 +152,7 @@ table and prints it out. #include <stdio.h> #include <search.h> #include <string.h> +#include <stdlib.h> struct info { /* This is the info stored in the table */ int age, room; /* other than the key. */ @@ -142,9 +163,8 @@ struct info { /* This is the info stored in the table */ int main(void) { - char string_space[NUM_EMPL*20]; /* Space to store strings. */ + char str[BUFSIZ]; /* Space to read string */ 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. */ @@ -154,12 +174,11 @@ main(void) /* Create table; no error checking is performed. */ (void) hcreate(NUM_EMPL); - while (scanf("%s%d%d", str_ptr, &info_ptr->age, + while (scanf("%s%d%d", str, &info_ptr->age, &info_ptr->room) != EOF && i++ < NUM_EMPL) { /* Put information in structure, and structure in item. */ - item.key = str_ptr; + item.key = strdup(str); item.data = info_ptr; - str_ptr += strlen(str_ptr) + 1; info_ptr++; /* Put item into table. */ (void) hsearch(item, ENTER); @@ -177,6 +196,7 @@ main(void) } else (void)printf("no such employee %s\en", name_to_find); } + hdestroy(); return 0; } .Ed |