Skip Navigation Links | |
Exit Print View | |
![]() |
Oracle Solaris Studio 12.3: C User's Guide Oracle Solaris Studio 12.3 Information Library |
1. Introduction to the C Compiler
2. C-Compiler Implementation-Specific Information
2.3 Thread Local Storage Specifier
2.4 Floating Point, Nonstandard Mode
2.6.1 Printing long long Data Types
2.6.2 Usual Arithmetic Conversions
2.7 Case Ranges in Switch Statements
2.11.3 does_not_read_global_data
2.11.5 does_not_write_global_data
2.11.26 warn_missing_parameter_info
2.13 Preserving the Value of errno
2.14.3 __inline and __inline__
2.14.5 __FUNCTION__ and __PRETTY_FUNCTION__
2.16 How to Specify Include Files
2.16.1 Using the -I- Option to Change the Search Algorithm
2.17 Compiling in Free-Standing Environments
2.18 Compiler Support for Intel MMX and Extended x86 Platform Intrinsics
7. Converting Applications for a 64-Bit Environment
8. cscope: Interactively Examining a C Program
A. Compiler Options Grouped by Functionality
B. C Compiler Options Reference
C. Implementation-Defined ISO/IEC C99 Behavior
E. Implementation-Defined ISO/IEC C90 Behavior
H. Oracle Solaris Studio C: Differences Between K&R C and ISO C
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.