Search code examples
clookup-tablesjump-table

Difference between a lookup table and a jump table


I know that jump tables are mainly used to create switch statements in assembly:

int a = 5;
switch (a){
  case 5:
  ...
   break;
  ...
} 

In this case, jump is just a pointer to a memory address which has instructions to do case 5 work.

If i'm not mistaking, a lookup table has pre calculated results in an array? so instead of writing code to calculate them you just return the array index? Sort of like a HashMap.

Above two sound very similar to me, are they basically the same thing? One points to instructions and the other returns pre calculated results?


Solution

  • If i'm not mistaking, a lookup table has pre calculated results in an array? so instead of writing code to calculate them you just return the array index? Sort of like a HashMap.

    Correct. What's stored at that index could be data, or a pointer to data, or a pointer to a function etc etc.

    A jump table is simply a look-up table where each index corresponds to a function, most commonly implemented in C as an array of function pointers.