Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

hello2k1

An array of jumps

This topic is 5869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 pointers

funcPtrType functions[3] = {foo,doSomething,third};
...
// get the index of the command to execute

cmd = GetNextCommand();
// get and execute the corresponding function pointer

functions[cmd]();


hope that helps

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!