Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an indirect goto statement?

In Clang API, there is a GotoStmt and an IndirectGotoStmt. There is very little explanation on the difference between these two kinds of goto statments. I know what a goto label; statement is. But what is an indirect goto statement? I want to know what that is in the context of C/C++ code, not necessarily just Clang. What does it mean syntactically to have an indirect goto statement? Can you provide a code example?

Edit: The following question is interesting.

Can you make a computed goto in C++

like image 336
Galaxy Avatar asked May 26 '19 19:05

Galaxy


People also ask

What is goto statement example?

A goto statement is allowed to jump within the scope of a variable length array, but not past any declarations of objects with variably modified types. The following example shows a goto statement that is used to jump out of a nested loop. This function could be written without using a goto statement.

How many types of goto statements are there?

There are two different styles of implementing the goto statement in C: Transferring the control from down to the top. Transferring the control from top to down.

What is goto statement?

The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.

What can I use instead of a goto statement?

From the early days of programming (i.e. assembler programming) the replacement for goto directives is so-called structured programming. This means using if-then-else statements, for- and while-loops, switch statements, break and continue, etc.


1 Answers

There is a GNU extension that allows taking an address of a label, storing it for later use, then goto that address at a later point. See https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html for details. Example:

    void *ptr;

    if(...)
        ptr = &&foo;
    else
        ptr = &&bar;

    /* ... */
    goto *ptr;

foo:
    /* ... */

bar:
    /* ... */

Clang supports that too, as it aims at being compatible with GCC.

The use of the above might be, for example, in implementing state machines.

like image 160
Yakov Galka Avatar answered Sep 17 '22 15:09

Yakov Galka