Programming with the APR - Using APR HashTables

1 minute read

This short article will show how to use the Apache Portable Runtime (APR) with it’s built in hashtable.


If you are in need to place data in a memory structure to access it quickly, you are probably best with a hash table data type. The Apache Portable Runtime (APR) luckily, along some others like e.g. apr_table_t or apr_array_header_t (both defined in apr_tables.h), provides such a data type to you.

Hashtables do have the big advantage that you can supply them any data type you like. The nect big thing with hashtables is that they are usually very efficient if the number of elements that they are holding grows.

You can download the example file here.

Let’s hash

In order to demonstrate hashtables we are using a very simple data structure which holds a date, a username and a password. We then set some values onto this data structure and save the created structure into the hashtable.

After storing that object into the hashtable we are reading the objects back from the hashtable and output it’s contents.

The c0de

Now that you do have some overview of what we are actually doing here, it’s time to show you some code. I think it pretty much speaks for itself.

You should however, if you are using strings as keys, use the special APR_HASH_KEY_STRING value to indicate a string valued key to APR. This will use strlen(key) to compute the length (NUL terminator is not included there).

Now go and hash some values …