3

Let me start by saying that I know how function pointers work. If you would like to explain them in more detail please go ahead, however what I ask from you is how can I implement them in a dispatch table using C.

I have searched for what a dispatch table is but wasn't really able to comprehend anything more than a vague understanding about how it might possibly work.

Please be kind enough to share it's practical uses and how I can create my own dispatch table in C. Help is very much appreciated.

HamzaKerem
  • 121
  • 1
  • 8
  • 7
    Why would you implement a dispatch table if you don't know what it is? – Fiddling Bits May 26 '20 at 17:26
  • 3
    @FiddlingBits: The question does not state OP wants to implement a dispatch table. It asks how they can. That is, it solicits knowledge. By and large, accumulating knowledge of all sorts is valuable. Once a person learns how to do something, they will have knowledge useful for determining when to do it, as well as knowledge possibly illuminating about related things. And Stack Overflow exists to answer questions like this. – Eric Postpischil May 26 '20 at 17:38
  • OP: *How can I implement it in C?* and *... how I can create my own dispatch table in C*. – Fiddling Bits May 26 '20 at 17:41
  • 2
    I'd disagree that SO exists to answer very generic questions. – Fiddling Bits May 26 '20 at 17:42
  • Also, I'd disagree that accumulating all sorts of knowledge, even what is irrelevant to you, is valuable. – Fiddling Bits May 26 '20 at 17:44
  • A dispatch table would generally be implemented as an array of function pointers. – Barmar May 26 '20 at 17:53
  • @FiddlingBits: The English words “How can I…” inquire, asking for information. They do not assert a desire to perform an act. – Eric Postpischil May 26 '20 at 17:55
  • @EricPostpischil I understand your point. – Fiddling Bits May 26 '20 at 17:56
  • I have have found something useful :https://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers – HamzaKerem May 26 '20 at 18:00
  • 1
    @mumcuhkm34 I think my question is valid. Why learn about something that is irrelevant to you? It may be relevant to you in the future, but not now. What I'm saying is, as your time is finite, focus on what you can use now. Sure, you may need to learn more in the future -- that's where a mentor (which SO is not, BTW) comes into play. – Fiddling Bits May 26 '20 at 18:00
  • Related wikipage: https://en.wikipedia.org/wiki/Virtual_method_table – Basile Starynkevitch May 26 '20 at 18:04
  • 1
    Mumcuhkm34, @FiddlingBits remarks were not personal. It was just a doubt concerning what questions are actually answered on SO, being your question very wide and general (without a specific implementation need). For example: is it clear why one should bother with the strange code provided by Eric instead of just calling functions themselves? – Roberto Caboni May 26 '20 at 18:14

1 Answers1

5

Dispatch tables can be implemented in several ways. One is with a table of pointers to functions:

int Add     (int a, int b) { return a + b; }
int Subtract(int a, int b) { return a - b; }
int Multiply(int a, int b) { return a * b; }
int Divide  (int a, int b) { return a / b; }


int DoFunction(int Select, int a, int b)
{
    /*  Declare a type to point to a function with parameters (int a, int b)
        and returning an int.
    */
    typedef int (*MyFunctionPointer)(int a, int b);

    //  Build a dispatch table with pointers to functions.
    MyFunctionPointer Table[] =
        {
            Add,
            Subtract,
            Multiply,
            Divide,
        };

    //  Dispatch to the requested function.
    return Table[Select](a, b);
}


#include <stdio.h>


int main(void)
{
    //  Demonstrate calls using dispatch table.
    printf("7 + 3 = %d.\n", DoFunction(0, 7, 3));
    printf("7 - 3 = %d.\n", DoFunction(1, 7, 3));
    printf("7 * 3 = %d.\n", DoFunction(2, 7, 3));
    printf("7 / 3 = %d.\n", DoFunction(3, 7, 3));
}

One might also jump into a table of branch instructions. This is more common in assembly than in higher level languages.

Essentially, a dispatch table is some method of transferring program control to a location selected via an index, rather than by individual selections such as with if or switch statements. In some situations, it is easier or cleaner to compute an index to select a function than to write some convoluted selection statements.

(This example shows homogeneous functions—they all have the same parameter type list and return type. If functions are not homogeneous, it may be trickier to use a dispatch table in C.)

Although dispatch tables are not frequently encountered in much source code (nor are they uncommon), they can be used for a variety of things, such as:

  • On some processors, interrupt service routines are handled through a dispatch table: There are fixed locations in memory where addresses of routines are stored, forming a table of addresses. When an interrupt occurs, the hardware looks up the address and transfer control to it.

  • In code that should be high-performance on a variety of hardware, we might prepare several functions that each use a different algorithm designed for particular hardware. When the program starts, it could test what hardware it is executing on (such as specific processor model) and record an index into the table. That index would indicate which routine to execute. Then calls to the function can use the index table for quick dispatching, without needing to test-and-branch for each call.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Much better than my contrived example. Thanks! :) – Retired Ninja May 26 '20 at 18:03
  • 1
    Interesting edit, my upvote is really well deserved. Just a note: tables containing jump addresses of ISRs are called "vector tables". I'm not sure they are universally called in that way, but it's the way I always called them. – Roberto Caboni May 26 '20 at 18:37