How to save goto tags in an array and then go to them?

I want to declare an array of "jumplabels".

Then I want to go to "jumplabel" in this array.

But I do not know how to do this.

It should look like this:

function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2] = s2; s0: .... goto s[v]; s1: .... goto s[v]; s2: .... goto s[v]; } 

Does anyone have an idea how to do this?

+17
c ++ c goto jump-table
Jun 02 '09 at 8:35
source share
11 answers

Perhaps using the GCC function, known as " labels as values ."

 void *s[3] = {&&s0, &&s1, &&s2}; if (n >= 0 && n <=2) goto *s[n]; s0: ... s1: ... s2: ... 

Only works with GCC!

+38
Jun 02 '09 at 8:49
source share
— -

goto requires a compile time stamp.

From this example, it seems that you are implementing some kind of state machine. Most often they are implemented as a switch design:

 while (!finished) switch (state) { case s0: /* ... */ state = newstate; break; /* ... */ } 

If you want it to be more dynamic, use an array of function pointers.

+16
Jun 02 '09 at 8:41
source share

There is no direct way to save coding addresses for switching to C. How about using a switch.

 #define jump(x) do{ label=x; goto jump_target; }while(0) int label=START; jump_target: switch(label) { case START: /* ... */ case LABEL_A: /* ... */ } 

You can find similar code created by each parser generator generator without a stack. Such a code is not easy to follow, so if it is not generated, or your problem is most easily described by the state machine, I would recommend not to do this.

+11
Jun 02 '09 at 8:40
source share

Can function pointers be used instead of goto?

Thus, you can create an array of functions to call and call the corresponding.

+8
Jun 02 '09 at 8:40
source share

In the normal C standard, this is not possible, as far as I know. However, there is an extension in the GCC compiler described here that makes this possible.

The extension introduces a new && operator to take the label address, which can then be used with the goto operator.

+6
Jun 02 '09 at 8:41
source share

What are switch ?

 switch (var) { case 0: /* ... */ break; case 1: /* ... */ break; default: /* ... */ break; /* not necessary here */ } 

Note that it is not necessarily translated into the jump table by the compiler.

If you really want to build a jump table yourself, you can use an array of function pointers.

+5
Jun 02 '09 at 8:42
source share

You cannot do this with goto - labels must be identifiers, not variables or constants. I don’t understand why you don’t want to use the switch here - it will probably be just as effective if that is what concerns you.

+2
Jun 02 '09 at 8:42
source share

You might want to look at setjmp / longjmp.

+2
Jun 02 '09 at 11:36
source share

For a simple answer, instead of getting compilers to do real stupid things, learn good programming techniques.

+1
Jun 02 '09 at 8:47
source share

Tokenizer? This is similar to what gperf did. No, take a look at that.

+1
Jun. 07 '09 at 10:00
source share

Optimizing compilers (including GCC) will compile the switch statement into a jump table (making the switch statement just as fast as the thing you are trying to build) IF the following conditions are true:

Your wiring closets (state numbers) start from scratch.

Your wiring closets are strictly increasing.

You don't skip integers in your switch cases.

There are enough cases when the transition table is actually faster (several dozen comparisons and errors in the method of checking each case with switch statements are actually faster than the transition table).

This has the advantage of allowing you to write your own code in standard C rather than relying on a compiler extension. He will work just as fast at GCC. It will also work just as fast on most optimizing compilers (I know the Intel compiler does this, not sure about Microsoft). And it will work, albeit more slowly, on any compiler.

+1
Apr 16 '16 at 18:25
source share



All Articles