---
title: Dispatch table
slug: dispatch-table
qid: 
snapshot: enwiki-001365382304
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1365382304
tags: [10637301, 23603337, 26220375, 27600972, 62131846]
---

# Dispatch table

> [!note] Provenance
> Snapshot `enwiki-001365382304` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1365382304> · Licensed CC BY-SA 4.0

In computer science, a dispatch table is a data structure that maps a series of known actions to their corresponding code, which can later be retrieved dynamically. This structure is commonly implemented either using an associative array to directly map method names to a corresponding function pointer, or a simple array of function pointers with known indices. Common use cases include the implementation of late binding and virtual polymorphism in object-oriented programming and the implementation of system calls.

In computer science, a dispatch table is a data structure that maps a series of known actions to their corresponding code, which can later be retrieved dynamically. This structure is commonly implemented either using an associative array to directly map method names to a corresponding function pointer, or a simple array of function pointers with known indices. Common use cases include the implementation of late binding and virtual polymorphism in object-oriented programming and the implementation of system calls. In operating system kernel design, system calls are commonly implemented using a dispatch table of indices mapping to corresponding privileged system actions. For example, in Linux, the sys_exit system call has an index of 60, and can be called by passing 60 into the appropriate processor register and triggering a program interrupt. Calls sys_exit with exit code 0 mov rax, 60 mov rdi, 0 syscall Since system calls only require a number to reference the underlying kernel action, they can be called from user space while in reality triggering code in kernel space. This improves system security by ensuring that privileged code can only be run through the narrow interface that each call provides. Define the table using one anonymous code-ref and one named code-ref my %dispatch = ( "-h" => sub { return "hello\n"; }, "-g" => \&say_goodbye ); sub say_goodbye { return "goodbye\n"; } Fetch the code ref from the table, and invoke it my $sub = $dispatch{$ARGV[0]}; print $sub ? $sub->() : "unknown argument\n"; Running this Perl program as perl greet -h will produce "hello", and running it as perl greet -g will produce "goodbye". // Dispatch table of actions as well as a fallback const actions = { action1() { console.log("action1 called"); }, action2() { console.log("action2 called"); }, action3() { console.log("action3 called"); }, fallback() { console.log("fallback called"); } }; function performAction(key) { const action = Object.hasOwn(actions, key) ? key : "fallback"; return actions[action](); } performAction("action1"); // outputs "action1 called" performAction("action3"); // outputs "action3 called" performAction("action5"); // outputs "fallback called" In Lua, tables can store any value, including functions, making it trivial to implement a simple dispatch table. -- Dispatch table of actions as well as a fallback local actions = { action1 = function () print('action1 called') end, action2 = function () print('action2 called') end, action3 = function () print('action3 called') end, fallback = function () print('fallback called') end } function perform_action(key) if actions[key] ~= nil then return actions[key]() end return actions.fallback() end perform_action('action1') -- outputs "action1 called" perform_action('action3') -- outputs "action3 called" perform_action('action5') -- outputs "fallback called"
