0

In the following C code, f calls g, and g calls h. Notice the goto in h, however: it will jump back to f if a satisfies a certain condition.

void h(int a)
{
    if (a > 10)
        goto out;
}

void g(int a, int b)
{
    h(a);
}

void f(int a, int b)
{
    g(a, b);
    return;
out:
    printf("b: %d\n", b);
}

My question is: how will the stack be if the goto is triggered? Will g and h be unstacked? And will f still print the right value of b? (or will it print it right only in some cases when I am lucky?)

(Please, I don't want to discuss if this is a good practice, or if this should be used at all. Also, consider that the actual code is complicated enough so that the compiler won't be smart enough to, e.g., optimize g out)

[I can give details on why I am doing this, if it matters -- I don't think it does]

vaulttech
  • 493
  • 1
  • 5
  • 15

4 Answers4

4

The question is void, because it simply cannot be done like this: you only can goto within a function, not between functions.

For jumping between functions, you can use setjmp/longjmp.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • There is also no need for a stack in C from the language standard. – too honest for this site Jun 20 '16 at 12:59
  • .. where the fun thing of `setjmp`/`longjmp` is that their designer was well aware of potential stack problems (in implementations that use it) so they were designed to *not* use the same stack as regular functions. (See lots and lots of Stack Overflow questions, such as [this one](http://stackoverflow.com/q/7969075/2564301).) – Jongware Jun 20 '16 at 13:01
4

This will result in undefined behavior in standard C.

From 6.8.6.1/1 of the C Language Standard:

The identifier in a goto statement shall name a label located somewhere in the enclosing function. A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

s7amuser
  • 827
  • 6
  • 18
1

A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function.

Labels are local to a single function, you cannot jump between different functions.

NOTE − In my opinion the use of goto statement is highly discouraged.

ref: http://www.tutorialspoint.com/cprogramming/c_goto_statement.htm

danilonet
  • 1,757
  • 16
  • 33
  • 3
    "Use of goto statement is highly discouraged" is a subjective. I agree to it only for the "general" case. There are cases where using `goto` is the easiest way to go, e. g. error handling. – glglgl Jun 20 '16 at 12:57
  • I agree @glglgl. The Linux kernel is full of examples where it is fit – Ishay Peled Jun 20 '16 at 12:58
  • `goto` has its applications **in C**. So no, it is not discouraged. That's what CS profs tell the students. The profs with some insight in practical programming later weaken this statement. – too honest for this site Jun 20 '16 at 12:58
  • @IshayPeled Linux Kernel is far from a good example to "how to code".... my personal opinion. – LPs Jun 20 '16 at 13:05
  • 1
    @glglgl Can agree, but is the easiest way for who?.. Probably for lazy coders... ;) – LPs Jun 20 '16 at 13:06
  • @LPs it is your personal opinion (; Dijkstra's take on this is gotos are a disaster and Linus take is its good. – Ishay Peled Jun 20 '16 at 13:08
  • Yes, obviously it is my personal opinion, .. :-) – danilonet Jun 20 '16 at 13:14
  • @LPs Or maybe programmers who think/know that `goto` is a lesser evil than several levels of indentation. – glglgl Jun 20 '16 at 14:52
  • @IshayPeled Dijkstra's take on this is gotos are a disaster in languages where you can avoid them easily. AFAIR he doesn't completely reject them. – glglgl Jun 20 '16 at 14:52
-1

You cannot do this since labels are local to each particular function. However, the nearest standard equivalent is the setjmp() and longjmp() pair of functions. That should work. :)

Arjun Raju
  • 14
  • 1
  • 3