Hash Table
A hash table maps keys to values by hashing the key to a bucket index. Average lookup, insert, and delete are O(1). A bad hash or a full table turns that into O(n).
Detailed Explanation
C has no built-in map. You write one, or you use a library. The core is an array of buckets and a hash function. index = hash(key) % cap picks a bucket. Two keys that land in the same bucket collide.
Separate chaining stores a linked list (or another array) per bucket. Open addressing stores entries in the table itself and probes on collision: linear, quadratic, or double hash. Chaining is simpler to delete from. Open addressing is more cache-friendly when the load is low and needs a tombstone or a rehash to delete.
The hash function has to spread keys. For strings, FNV-1a or a well-known string hash is fine for general use. For integers, a multiplicative mix beats using the integer unchanged if the keys are patterned. Do not use hash = key % cap on addresses if cap is a power of two and the addresses are aligned; the low bits are zeros. Cryptographic hashes are slower than you need for a hash table unless the keys are hostile and you care about collision attacks.
Load factor is n / cap. When it climbs past about 0.7 for open addressing, or when chain lengths grow, you allocate a larger array and reinsert every live entry. Rehash is O(n) and is the spike in an otherwise O(1) API. If a latency deadline cannot take that spike, pre-size the table.
Equality is separate from hashing. Two keys with the same hash still have to be compared. For strings that is strcmp. For structs it is a field-wise compare. If you forget and compare pointers, two equal strings at different addresses miss.
Keys the table stores may be copies or borrowed pointers. A copy owns its bytes and must free them when the entry dies. A borrowed pointer must outlive the entry. Mixing the two in one table without a flag is a leak or a double-free.
Code Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Entry {
char *key;
int value;
struct Entry *next;
};
enum { CAP = 8 };
static unsigned hash_str(const char *s)
{
unsigned h = 2166136261u;
for (; *s; s++) {
h ^= (unsigned char)*s;
h *= 16777619u;
}
return h;
}
static int tab_put(struct Entry **tab, const char *key, int value)
{
unsigned i = hash_str(key) % CAP;
for (struct Entry *e = tab[i]; e != NULL; e = e->next) {
if (strcmp(e->key, key) == 0) {
e->value = value;
return 0;
}
}
struct Entry *e = malloc(sizeof *e);
if (e == NULL) {
return -1;
}
e->key = malloc(strlen(key) + 1);
if (e->key == NULL) {
free(e);
return -1;
}
memcpy(e->key, key, strlen(key) + 1);
e->value = value;
e->next = tab[i];
tab[i] = e;
return 0;
}
int main(void)
{
struct Entry *tab[CAP] = { 0 };
tab_put(tab, "age", 42);
unsigned i = hash_str("age") % CAP;
printf("%d\n", tab[i]->value); /* prints: 42 */
for (unsigned b = 0; b < CAP; b++) {
while (tab[b] != NULL) {
struct Entry *n = tab[b]->next;
free(tab[b]->key);
free(tab[b]);
tab[b] = n;
}
}
return 0;
}Common Gotchas
A table that stores borrowed keys dangles when the caller frees them
copy the key if the table must outlive the call.
`% cap` with a weak hash clusters
measure chain lengths. If one bucket owns most entries, fix the hash or resize.
Rehash invalidates pointers to entries in an open-addressed table
do not hand out interior pointers unless you document "no insert until you are done."