Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

2.5 Labels as Values

The C compiler recognizes the extension to C known as computed goto. Computed goto enables runtime determination of branching destinations. The address of a label can be acquired by using the ’&&’ operator and assigned to a pointer of type void *:

void *ptr;
...
ptr = &&label1;

A later goto statement can branch to label1 through ptr:

goto *ptr;

Because ptr is computed at runtime, ptr can take on the address of any label that is in-scope and the goto statement can branch to it.

One way of using computed goto is for the implementation of a jump table:

static void *ptrarray[] = { &&label1, &&label2, &&label3 };

Now the array elements can be selected by indexing:

goto *ptrarray[i];

Addresses of labels can be computed only from the current function scope. Attempting to take addresses of labels out of the current function yields unpredictable results.

The jump table works similarly to a switch statement although the jump table can make it more difficult to follow program flow. A notable difference is that the switch-statement jump-destinations are all in the forward direction from the switch reserved word. Using computed goto to implement a jump table enables branching in both forward and reverse directions.

#include <stdio.h>
void foo()
{
  void *ptr;

  ptr = &&label1;

  goto *ptr;

  printf("Failed!\n");
  return;

  label1:
  printf("Passed!\n");
  return;
}

int main(void)
{
  void *ptr;

  ptr = &&label1;

  goto *ptr;

  printf("Failed!\n");
  return 0;

  label1:
  foo();
  return 0;
}

The following example also makes use of a jump table to control program flow:

#include <stdio.h>

int main(void)
{
  int i = 0;
  static void * ptr[3]={&&label1, &&label2, &&label3};

  goto *ptr[i];

  label1:
  printf("label1\n");
  return 0;

  label2:
  printf("label2\n");
  return 0;

  label3:
  printf("label3\n");
  return 0;
}

%example: a.out
%example: label1

Another application of computed goto is as an interpreter for threaded code. The label addresses within the interpreter function can be stored in the threaded code for fast dispatching.