Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

goto

28 views
Skip to first unread message

bob

unread,
Sep 19, 2012, 12:00:19 PM9/19/12
to
In most languages that support goto, can you do a goto from one function to another?

diego

unread,
Sep 19, 2012, 12:28:13 PM9/19/12
to
On Wednesday, September 19, 2012 9:30:19 PM UTC+5:30, bob wrote:
> In most languages that support goto, can you do a goto from one function to another?

scope of the label is the entire function. So it's not possible.

diego

unread,
Sep 19, 2012, 12:28:55 PM9/19/12
to
On Wednesday, September 19, 2012 9:58:13 PM UTC+5:30, diego wrote:
> On Wednesday, September 19, 2012 9:30:19 PM UTC+5:30, bob wrote: > In most languages that support goto, can you do a goto from one function to another? scope of the label is the entire function. So it's not possible.

Atleast is C. I'm not sure about other languages.

Patricia Shanahan

unread,
Sep 19, 2012, 12:35:09 PM9/19/12
to
On 9/19/2012 9:00 AM, bob wrote:
> In most languages that support goto, can you do a goto from one function to another?
>

A function call usually involves some set-up that is done as part of
function call, such as pushing a stack frame to contain the local
variables, and remembering where to return to. Jumping into the middle
of the function without doing that set-up would cause problems.

The closest I know of are coroutines.

Are you trying to achieve something specific, or just curious?

Patricia

BartC

unread,
Sep 19, 2012, 2:25:01 PM9/19/12
to


"Patricia Shanahan" <pa...@acm.org> wrote in message
news:9dadnbInqY6nacTN...@earthlink.com...
> On 9/19/2012 9:00 AM, bob wrote:
>> In most languages that support goto, can you do a goto from one function
>> to another?
>>
>
> A function call usually involves some set-up that is done as part of
> function call, such as pushing a stack frame to contain the local
> variables, and remembering where to return to. Jumping into the middle
> of the function without doing that set-up would cause problems.
>
> The closest I know of are coroutines.

I do it all the time in one of my languages. But you need to know what is
happening.

In my case, I use it for threaded code, where you do a goto, not a call, to
the start of one function, and that then terminates by doing a goto, not a
return or call, to the next function.

There are a few other uses, but it's usually too risky to do with a
language/compiler you're not familiar with, even if there is some way of
doing it.

--
Bartc

Anonymous

unread,
Sep 20, 2012, 1:04:06 PM9/20/12
to
On 9/19/2012 9:00 AM, bob wrote:
> In most languages that support goto, can you do a goto from one function
> to another?

Yes, but you won't like what happens next.

bob

unread,
Sep 20, 2012, 3:36:18 PM9/20/12
to
I heard a rumor that some guy was doing this in C or C++ and shocking his fellow developers.

I was a little skeptical.

Robin Vowels

unread,
Sep 20, 2012, 9:37:52 PM9/20/12
to
On Sep 20, 2:00 am, bob <b...@coolfone.comze.com> wrote:
> In most languages that support goto, can you do a goto from one function to another?

No, because that would bypass procedure prologue.

That's what CALL and function references do.

Daniel Pitts

unread,
Sep 21, 2012, 1:35:03 PM9/21/12
to
There is, instead, a long-jmp, but that is a way to jump back up the
stack to a point and a state that was prepared for such a jump.

Mok-Kong Shen

unread,
Sep 22, 2012, 4:48:37 PM9/22/12
to
Am 19.09.2012 18:00, schrieb bob:
> In most languages that support goto, can you do a goto from one function to another?

I suppose that your question is not very clearly stated. What do you
mean by "goto from one function to another"? If that means to start
that function "normally", i.e. starting its execution from its
beginning, then you just call that another function (that call may
later return to the caller or never return to it, it depends). If you
want instead to start at some other (inner) points of that another
function, then IMHO you have to structure that function in such a way
that there is a branch at its beginning depending on the value of a
parameter of the function call. (Of course in assembler wild jumps are
easy, but one of the design goals of decent programming languages is
to prevent the possible bad consequences of such things from occurring.)

M. K. Shen


Mok-Kong Shen

unread,
Sep 22, 2012, 5:14:33 PM9/22/12
to

Addendum: In Fortran 90 (I don't have documents of later versions
of Fortran at hand to say whether that feature remains in the later
versions) there is the ENTRY statement for a subroutine subprogram.
It is stated there:

An ENTRY statement permits a procedure reference to begin
with a particular executable statement within the function
or subroutine subprogram in which the ENTRY statement appears.

However the use of ENTRY statement is constrained in such a way
that wild jumps as in the case of assembler are impossible.

M. K. Shen

Patricia Shanahan

unread,
Sep 22, 2012, 7:12:58 PM9/22/12
to
Also, if I remember correctly, an alternative entry point in a Fortran
subprogram is accessed via a call, not a goto.

Patricia

Mok-Kong Shen

unread,
Sep 23, 2012, 9:25:17 AM9/23/12
to
Am 23.09.2012 01:12, schrieb Patricia Shanahan:
> On 9/22/2012 2:14 PM, Mok-Kong Shen wrote:
>>
>> Addendum: In Fortran 90 (I don't have documents of later versions
>> of Fortran at hand to say whether that feature remains in the later
>> versions) there is the ENTRY statement for a subroutine subprogram.
>> It is stated there:
>>
>> An ENTRY statement permits a procedure reference to begin
>> with a particular executable statement within the function
>> or subroutine subprogram in which the ENTRY statement appears.
>>
>> However the use of ENTRY statement is constrained in such a way
>> that wild jumps as in the case of assembler are impossible.

> Also, if I remember correctly, an alternative entry point in a Fortran
> subprogram is accessed via a call, not a goto.

You are completely right. That's no longer at the level of a simple
goto. The document has the following:

If the ENTRY statement is contained in a subroutine subprogram,
an additionl subroutine is defined by that subprogram. The
name of the subroutine is entry-name. The dummy arguments of
the subroutine are those specified on the ENTRY statement.

So one is effectively lumping a number of subroutines into one,
creating thereby though certain direct connections between them
due to the codes.

M. K. Shen

Florian Scholz

unread,
Sep 28, 2012, 12:36:09 PM9/28/12
to
Am 19.09.2012 18:00, schrieb bob:
> In most languages that support goto, can you do a goto from one function to another?
Do you really want this? I guess in C/C++ you would destroy your stack.


bob

unread,
Sep 28, 2012, 6:05:37 PM9/28/12
to
Well, I finally put it to the test with this C++ program.

#include <iostream>

void test(void)
{
another_function:
std::cout << "Hello, World!\n";

}

int main(int argc, const char * argv[])
{
goto another_function;
return 0;
}

It does not compile, so I guess you can't do it.

Doesn't work in C either.

Sounds like the urban legend I heard was probably hyperbole or hogwash.

BartC

unread,
Sep 28, 2012, 6:39:36 PM9/28/12
to
"bob" <b...@coolfone.comze.com> wrote in message
news:cec59517-7a04-469c...@googlegroups.com...

> Well, I finally put it to the test with this C++ program.

> It does not compile, so I guess you can't do it.
>
> Doesn't work in C either.
>
> Sounds like the urban legend I heard was probably hyperbole or hogwash.

The tricky bit is making the label accessible outside it's scope. Try this
in gcc:

#include <stdio.h>

int glabel;

void fn1(int setup) {
if (setup) {
glabel=(int)&&lab1;
return;
}
puts("one");
puts("two");
lab1:
puts("three");
puts("four");
}

int main (void){

fn1(1); /* Copy the label address into the global */

goto *glabel;
}

It should print "three" then "four" after jumping into the function.

However, while my test didn't crash, I can't guarantee what yours will do!

--
Bartc


Robin Vowels

unread,
Sep 28, 2012, 9:07:49 PM9/28/12
to
An ENTRY statement is available in PL/I also.
Like the normal procedure entry, it can be invoked only by a normal
procedure call
or by a function reference.
In all cases, the standard prologue for the procedure is executed
(such as allocating storage, establishing a return address, etc).

As I said in a previous post, in general, a simple GO TO statement can
never do that
and thus is prohibited.
0 new messages