Does anyone know how to implement "computed goto" in C or C++ in MSVC. Instead of jumping to a label, I want to jump using a variable that stores the value of label.
GCC has this concept of "label value operator" which allows storing the address of a label in a variable and then use that variable in the GOTO statement. I could not find something equivalent in MS VC++.

I need to implement something like a jump table in VC++ and appreciate all your hints and recommended techniques.

thank you,

Re: Visual C++ Language GOTO label variables

Holger Grund

That doesn't work in VC and as someone having had the fun of implementing that feature for a compiler that strives to reach 100% source compatibility with GCC: don't use that feature if you avoid it.

A switch would be a much better idea. I don't even see a reason to use indirect gotos and address label jump tables in GCC for non-PIC (only IA-64 Windows supports PIC). Modern compiler will more efficiently translate switches to jump tables.


Re: Visual C++ Language GOTO label variables


The computed goto has struck me as being an attempt at writing source in ways more cunning than necessary. My point being that it'd usually make the code more readable (and thus better in oh-so-many-ways) if the gotos were replaced with a function dispatch mechanism (using a hashmap, e.g.), switch blocks (as suggested), or even a load of ifs. That's my opinion, anyway.

Re: Visual C++ Language GOTO label variables


I agree with both in terms of readability and portability. But my problem kind of requires this. I'm trying to implement compiled simulation for a processor. The idea is to get the stream of instructions and translate them back to C. Then compile that C on PC and run it. This is a lot faster than interpreting binary instructions.
Since the code may have indirect jumps, I needed that trick to actually make the generated code more readable! (better correspondence to the original assembly!).

I have already considered somehow generating large switch blocks, but it makes a real mess!

I was hoping someone has a better suggestion.


Re: Visual C++ Language GOTO label variables

Paul Marriott

I believe you are talking about function pointers.

Thus you can build an array using the function pointers and use an offset to index the particular function required.

A sample page on how to use is

I have used myself, but I had a static array of functions for output routines, and then called them based on an index.

It would have been the same as using if or switch statements but made the calling much more convenient.