An array of jumps

Started by
12 comments, last by hello2k1 21 years, 4 months ago
After something like 2 hours of searching, and talking to my partener, I just can''t figure out how to do something like: jumps jArray[]={j1, j2, j3, j4}; goto jArray[2]; j1: j2: j3: j4: I have tried using defines, treating it as raw memory, etc.. But nothing seems to compile. Right now I am considering using x86, but am trying not to do so because of the new 64 bits standards being introduced now.
------------------------------There are 10 types of people in this world, those who know binary, and those who don't.
Advertisement
The reason you cannot do what you want is simple ... a goto jumps to a location denoted by a LABEL ... which only has a meaning at compile time. There is no data type for a label, with which you can put it in a (run time) array, and then use it.

The closest thing you are looking for is simply pointers, often even, function pointers .... and you make a jump table, by filing an array of function pointers with pointers which make sense based on a particular situation, such as (sloppy example)


  //I can''t remember function ptr systax, but assume you have// functions: foo(), doSomething(), third(), which all follow// the prototype which matches a typedef alias ''funcPtrType''// then you might do this:// prepare the array of function pointersfuncPtrType functions[3] = {foo,doSomething,third};...// get the index of the command to executecmd = GetNextCommand();// get and execute the corresponding function pointerfunctions[cmd]();  


hope that helps
You could code a jump-table in assembly... but what are you trying to do? There''s probably a C or C++ way to do it (switch, vtable/inheritence, function pointers).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Another alternative: look at longjmp() and setjmp()

The problem as Xai points out is that the labels dissolve into nothing. The optimiser might even reorganise the program flow.

You don''t really specify any context, but what you''ve posted is nothing more than a switch...case, even if you intend changing the contents of jArray, surely it could still be done that way.

Other than that, an array of function pointers would get my next vote

A few other slightly sick & twisted methods spring to mind, such as locally defined structures with the labels inside etc...

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

I could use a switch, but the reason I don''t want to is because there could easily become 100s of items in the list. Is there anything you guys can think of thatd wor in this case?
------------------------------There are 10 types of people in this world, those who know binary, and those who don't.
There is nothing I can think of that''ll do what you want. What I would suggest respectfully though, is that you make a study of structured programming. Using structured programming techniques you should find that you will never need to use "goto" in your programs. In the long run, structured programming techniques will help you to design and implement bigger and more complex programs. The continued use of "goto" will (imho) only serve to hinder your progress as a programmer.

Roo, Pantheon Software
quote:Original post by hello2k1
After something like 2 hours of searching, and talking to my partener, I just can''t figure out how to do something like:

jumps jArray[]={j1, j2, j3, j4};
goto jArray[2];
j1:
j2:
j3:
j4:

Oh, come off it...

  void j1() { /* ... */ }void j2() { /* ... */ }void j3() { /* ... */ }void j4() { /* ... */ }int main(){  j2();}  

quote:
Right now I am considering using x86, but am trying not to do so because of the new 64 bits standards being introduced now.

It looks to me like you *are* using assembly language, just via a C compiler.
Ok, first of all, comments like that annoy me. I have been programming for 4 years now, own a gaming company, and am currently doing contract work for another gaming company. Goto statements are simply what popped into my head at the moment, and I am well aware the problems associated with them. Structured programming is exactly what I''m trying to avoid in this instance, simply because it will cost me a very large amount of cpu cycles - which we simply can''t afford to waste.

Also, it may seem as though I''m using asm, but in fact I am not since none of the parils are associated with it. x86 assembly is quickly becoming de-standartized, and I would not want to be stuck in the middle of the 64-bit problem.

Even though x86 may become problematic (and ROM material), I feel that it may be the only way to surpass this problem. If anyone has any suggestions, I would really like to hear them.

Thank you.
------------------------------There are 10 types of people in this world, those who know binary, and those who don't.
quote:Original post by hello2k1
I could use a switch, but the reason I don''t want to is because there could easily become 100s of items in the list. Is there anything you guys can think of thatd wor in this case?

A good compiler will make a jump table out of a switch.


Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
It''s difficult to suggest a solution if you don''t say why exactly you don''t want to use switch/case or function pointers.

I don''t see the major difference in what you mention in your original post and an array of function pointers or a switch statement.
<a href="http://www.purplenose.com>purplenose.com

This topic is closed to new replies.

Advertisement