Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there no hashtables in the C standard library?

Why is that there is no Hashtable support as part of Standard C Library? Is there any specific reason for this?

like image 218
Shankar Raju Avatar asked May 25 '11 01:05

Shankar Raju


People also ask

Are there Hashmaps in C?

Hash Table is a data structure which stores data in an associative manner. In hash table, the data is stored in an array format where each data value has its own unique index value.

When should you not use Hashtables?

There are some operations which are not efficiently supported by hash tables, such as iterating over all the elements whose keys are within a certain range, finding the element with the largest key or smallest key, and so on.

Does C have a hash table?

A Hash Table in C/C++ (Associative array) is a data structure that maps keys to values. This uses a hash function to compute indexes for a key.

Where are Hashtables used?

Uses: They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.


1 Answers

C seems unusual by today's standards because there are no useful data structures defined. None. Not even strings — and if you think a C string is a data structure, well, we'll have to disagree on what a "data structure" is.

If you like C, then think of it as a "blank slate"... your entire application is made of code written by you and libraries you choose to pull in, plus a few fairly primitive standard library functions, with maybe one or two exceptions like qsort. People use C these days to implement things like Python, Ruby, Apache, or the Linux kernel. These are projects that use all of their own data structures anyway, and they wouldn't be likely to use something like the STL.

Many C libraries implement generic hash tables. There are tradeoffs, and you can pick your favorite. Some of them are configurable using callbacks.

  • Glib has a hash table object (documentation)
  • Apache Portable Runtime has a hash table (documentation)
  • Apple's Core Foundation library has a hash table (documentation) Note: Yes you can insert any object as key or value.
  • UTHash is a hash table library (documentation)
  • Another hash table library (link)

With all of these libraries that do what you want, what's the point of adding a hash table to the C standard?

like image 175
Dietrich Epp Avatar answered Sep 23 '22 02:09

Dietrich Epp