PROGMEM function pointer array

This is a trick useful when you have to implement a lot of functions indexed in some dynamic way.

The usual way to handle this was using a switch statement which can become too long and complicated to handle. By putting all the functions into an PROGMEM array we achieve three improvements at the same time:

  • We are forced to split all functions from the switch block, improving the readability and ease of maintenance.
  • The switch block itself is gone, resulting in more readable code
  • The resulting assembly code is a pointer dereference instead of a sequential search, reducing the computational complexity from O(n) to O(1) or constant time.

Have you compared this to a virtual member function call (using a C++ class)?

Cheers!