473,597 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a jump table in c or c++ ?

Hi,

can someone tell me how i do create a jump table in c or c++ ?
I tried this:

void f()
{
void *jump_table[] =
{
label1,
label2
};

goto jump_table[1];

label1:
// some code ...
label2:
// some code ...
}

It doesn't work,
the compiler (VC6) refuses to assign the labels to the array.
Please help me !

Andreas
Jul 22 '05 #1
2 3839

"Andreas" <an************ **@freenet.de> wrote in message
news:16******** *************** **@posting.goog le.com...
Hi,

can someone tell me how i do create a jump table in c or c++ ?
I tried this:


You can't get the address of a label. Your options are to either use
functions and an array of function pointers, or use a switch statement.

It looks like in your case a switch statement is what you are looking
for:
void f() {
switch(1) {
case 1:
// some code...;
case 2:
// some code...
}
}

In actuallity, the given a reasoable ratio of the number of labels to the
range of choices most
compilers will generate a jump table out of that code (otherwise it tends to
devolve into the
logical equivelent of a bunch of if() statement). I checked in the g++
code once for the threshold
for making that decision and it almost always favors making the jump table.
You have to do something
really bizaare like:

switch(i) {
case 1: // code.
case 425824356: // code
}
to force it the other way.
Jul 22 '05 #2
"Andreas" <an************ **@freenet.de> wrote...
can someone tell me how i do create a jump table in c or c++ ?
No such thing in either language, I'm afraid. You cannot have
an array of labels.

You can, however, create and use a _call_ table. All you need
is an array of pointers to functions, and then call the indexed
one:

void f1() {
// do something
}

void f2() {
// do something else
}

void f() {
void (*ptrs[])() = { f1, f2 };
ptrs[1](); // calls f2
}
I tried this:

void f()
{
void *jump_table[] =
{
label1,
label2
};

goto jump_table[1];

label1:
// some code ...
label2:
// some code ...
}

It doesn't work,
the compiler (VC6) refuses to assign the labels to the array.


And so will every sane C++ compiler on the planet.

Victor
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5856
by: Gamesetal | last post by:
Have '97 and am trying to create a query on the underlying Hyperlink field - ie if xxx then else ... Can't find a way to do this and would very much appreciate some kind help - thanks - John
8
13949
by: Horst Walter | last post by:
I create an Excel worksheet in C# (should be similar in VB) Connection String: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";" + "Extended Properties=Excel 8.0;"; Create string: "CREATE TABLE S1 (Dt date, St char(40), Cr currency)" Works fine, but the worksheet is named _S1 (UNDERSCORE!) and not S1.
9
3860
by: paul c | last post by:
Apologies if I'm sending this to the wrong group. If so I'd be grateful if somebody could point me to the right one. I'm using microsoft visual c++ 6.0 compiler. My code is C, I just use the c++ compiler for easier positioning of declarations here and there. I am putting together a small interpreter that will do a great deal of iteration and I'd like to avoid using function pointers to my opcode
4
4223
by: Steve | last post by:
C# I am iterating through a collection of rows thus: foreach(DataRow myRow in dsTable.Table.Rows) { ........... } in this loop I have some conditions and when one is met I dont want it to
6
5028
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and not the address of the function itself. Are there any BKMs for getting the real address or am I going to have to write a function that looks to see whether the address is a jump instruction and if so compute the real address from the jump target?...
1
1290
by: blee via AccessMonster.com | last post by:
Hello and thanks for previous help. Still pretty new to this (KISS), but learning... I have a table with a list of athletes, an autoID unique #, parents, address, phone #'s. I have a second table which holds information about injuries which occur in the season, which has athleteID (from previous table), location of injury, etc; this data is entered via a form. The problem is now that my list of athletes is growing (will be 600 or so),...
2
1708
by: Sean Staniforth | last post by:
I have a Database which contains anout 20 tables. each has different data. for a research centre studying Family History. I want to create a simple from end that researchers can entre a surname & First name to find their ancestor, this will give them reference number relation to the house Then they will want to clear the two names and enter the reference numbers (town / Village / house) that will give census details of those living at...
9
10989
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to how a switch case like the following one is converted into a jump table by the compiler, assuming that it does, switch(i) { case 4 : { ... printf("4");
3
5498
by: dragon52 | last post by:
Hi, When the user click a button on screen I display a table of info way down the page and I like to immediately jump to that section of the page, ie without the user having to scroll down to see the table. How do I do that? I know how to in the static way like a menu but not linking the jump to a button_click. Can anyone help?? thanks
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.