473,480 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Create 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 3818

"Andreas" <an**************@freenet.de> wrote in message
news:16*************************@posting.google.co m...
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
5847
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
13934
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...
9
3850
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...
4
4207
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
5009
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...
1
1279
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...
2
1696
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 &...
9
10969
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
5487
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...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
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...
0
7032
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,...
0
7076
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...
1
6730
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...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
2990
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...
0
1294
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 ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.