Reference articles on history, science, culture and more
Encyclopedia

Dispatch table

Computer code structure with a table of pointers to functions or similar

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.

01Use cases

Virtual methods

In object-oriented programming languages that support virtual methods, the compiler will automatically create a dispatch table for each class that maps method names to their corresponding code. This table is called a virtual method table or vtable, and a pointer to the table is stored in every object created from that class, called a virtual pointer or vptr. Since an object always has direct access to its methods using the vptr, virtual methods can be dynamically called at runtime without needing to know the specific class that the object is constructed from. This layer of indirection makes polymorphism and late binding possible, which is a fundamental aspect of object-oriented programming.

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.

02Example implementations

Perl

The following shows one way to implement a dispatch table in Perl, using a hash to store code references (also known as function pointers).

# 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".

JavaScript

A dispatch table can be implemented in JavaScript by using an object to store anonymous function definitions.

// 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"

Lua

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"
Watch videos about Dispatch tableExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Dispatch table, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.