Skip to content

Instantly share code, notes, and snippets.

@chaitanyav
Created August 24, 2013 15:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaitanyav/6328707 to your computer and use it in GitHub Desktop.
Save chaitanyav/6328707 to your computer and use it in GitHub Desktop.
/*
* Author: NagaChaitanya Vellanki
*
* Hash table example
*/
#include <errno.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
ENTRY item;
ENTRY *found_item;
/* create hash table */
if(hcreate(3) == 0) {
printf("Error on hcreate, %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
item.key = "1";
item.data = "foo";
hsearch(item, ENTER);
item.key = "2";
item.data = "bar";
hsearch(item, ENTER);
item.key = "3";
item.data = "goo";
hsearch(item, ENTER);
item.key = "4";
item.data = "bird";
// created a hash table of size 3, ideally entering a 4th item should
// return NULL but if it does not the system must have a created a hash
// table of bigger size for performance reasons
if(hsearch(item, ENTER) == NULL) {
printf("Erron on hsearch, %s\n", strerror(errno));
}
item.key = "2";
found_item = hsearch(item, FIND);
if(found_item != NULL) {
printf("key: %s, data: %s\n", found_item->key, (char *)found_item->data);
} else {
printf("key: %s was not found\n", item.key);
}
item.key = "5";
found_item = hsearch(item, FIND);
if(found_item != NULL) {
printf("key: %s, data: %s\n", found_item->key, (char *)found_item->data);
} else {
printf("key: %s was not found\n", item.key);
}
hdestroy();
exit(EXIT_SUCCESS);
}
@tahseenjamal
Copy link

tahseenjamal commented Aug 5, 2018

on OSX 10+

gcc -o hash_table_example hash_table_example.c

./hash_table_example

key: 2, data: bar
key: 5 was not found
another(41701,0x7fffa02a3380) malloc: *** error for object 0x10ab6ff58: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

on Ubuntu 17.10

gcc -o hash_table_example hash_table_example.c

./hash_table_example

Erron on hsearch, Cannot allocate memory
key: 2, data: bar
key: 5 was not found

@twodayslate
Copy link

@tahseenjamal the macOS variant of hdestroy free's the keys

@YouZhengChuan
Copy link

on MacOS:

» gcc -v
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: x86_64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
» gcc -o hash_table_example hash_table_example.c && ./hash_table_example
key: 2, data: bar
key: 5 was not found
hash_table_example(88733,0x2042bc600) malloc: *** error for object 0x1040f3f54: pointer being freed was not allocated
hash_table_example(88733,0x2042bc600) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    88733 abort      ./hash_table_example

on Centos 7 is OK:

# LANG=C gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
# gcc -o hash_table_example hash_table_example.c && ./hash_table_example
Erron on hsearch, Cannot allocate memory
key: 2, data: bar
key: 5 was not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment