Here is an alternative idea that has the advantage of speed, having some memory overhead.
Basically, the simplest form of a hash table is where a hash function is an identifier (code → code), also known as a lookup table.
To do this, knowing that HTTP status codes are limited to 5xx, you can assume that 599 will be the highest you need, so you will create a table with 600 elements.
This table can be done as follows:
const char * status_messages[600];
Initialization is quite simple:
memset(status_messages, (int)NULL, 600 * sizeof(const char *)); status_messages[403] = "Forbidden"; status_messages[404] = "Not found";
Raising a message is also very simple:
int code = 403; const char * message = status_messages[code];
This array will have a size of 2400 bytes (4800 on 64-bit platforms), but O (1) access time is guaranteed.
SirDarius
source share