Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #2226 > unrolled thread

goto

Started bybob <bob@coolfone.comze.com>
First post2012-09-19 09:00 -0700
Last post2012-09-28 23:39 +0100
Articles 17 — 9 participants

Back to article view | Back to comp.programming


Contents

  goto bob <bob@coolfone.comze.com> - 2012-09-19 09:00 -0700
    Re: goto diego <deepakpjose@gmail.com> - 2012-09-19 09:28 -0700
      Re: goto diego <deepakpjose@gmail.com> - 2012-09-19 09:28 -0700
        Re: goto bob <bob@coolfone.comze.com> - 2012-09-20 12:36 -0700
    Re: goto Patricia Shanahan <pats@acm.org> - 2012-09-19 09:35 -0700
      Re: goto "BartC" <bc@freeuk.com> - 2012-09-19 19:25 +0100
      Re: goto Anonymous <nobody@remailer.paranoici.org> - 2012-09-20 17:04 +0000
    Re: goto Robin Vowels <robin.vowels@gmail.com> - 2012-09-20 18:37 -0700
      Re: goto Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-21 10:35 -0700
    Re: goto Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-09-22 22:48 +0200
      Re: goto Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-09-22 23:14 +0200
        Re: goto Patricia Shanahan <pats@acm.org> - 2012-09-22 16:12 -0700
          Re: goto Mok-Kong Shen <mok-kong.shen@t-online.de> - 2012-09-23 15:25 +0200
        Re: goto Robin Vowels <robin.vowels@gmail.com> - 2012-09-28 18:07 -0700
    Re: goto Florian Scholz <florian.scholz@stud.hn.de> - 2012-09-28 18:36 +0200
      Re: goto bob <bob@coolfone.comze.com> - 2012-09-28 15:05 -0700
        Re: goto "BartC" <bc@freeuk.com> - 2012-09-28 23:39 +0100

#2226 — goto

Frombob <bob@coolfone.comze.com>
Date2012-09-19 09:00 -0700
Subjectgoto
Message-ID<04ef7083-e585-41b5-a7b2-9752597583d6@googlegroups.com>
In most languages that support goto, can you do a goto from one function to another?

[toc] | [next] | [standalone]


#2227

Fromdiego <deepakpjose@gmail.com>
Date2012-09-19 09:28 -0700
Message-ID<439884b4-7cf6-4807-bc3b-e4c017dfcdf6@googlegroups.com>
In reply to#2226
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.

[toc] | [prev] | [next] | [standalone]


#2228

Fromdiego <deepakpjose@gmail.com>
Date2012-09-19 09:28 -0700
Message-ID<db1df69b-6725-4949-a0bd-6e96d8208b24@googlegroups.com>
In reply to#2227
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.

[toc] | [prev] | [next] | [standalone]


#2232

Frombob <bob@coolfone.comze.com>
Date2012-09-20 12:36 -0700
Message-ID<b3c03497-1ca6-41e2-8147-34fdd789ec3f@googlegroups.com>
In reply to#2228
On Wednesday, September 19, 2012 11:28:55 AM UTC-5, diego wrote:
> 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.

I heard a rumor that some guy was doing this in C or C++ and shocking his fellow developers.

I was a little skeptical.

[toc] | [prev] | [next] | [standalone]


#2229

FromPatricia Shanahan <pats@acm.org>
Date2012-09-19 09:35 -0700
Message-ID<9dadnbInqY6nacTNnZ2dnUVZ_oqdnZ2d@earthlink.com>
In reply to#2226
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

[toc] | [prev] | [next] | [standalone]


#2230

From"BartC" <bc@freeuk.com>
Date2012-09-19 19:25 +0100
Message-ID<k3d2n5$n75$1@dont-email.me>
In reply to#2229

"Patricia Shanahan" <pats@acm.org> wrote in message 
news:9dadnbInqY6nacTNnZ2dnUVZ_oqdnZ2d@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 

[toc] | [prev] | [next] | [standalone]


#2231

FromAnonymous <nobody@remailer.paranoici.org>
Date2012-09-20 17:04 +0000
Message-ID<30b4d53147d2dc1986885515fc966563@remailer.paranoici.org>
In reply to#2229
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.

[toc] | [prev] | [next] | [standalone]


#2233

FromRobin Vowels <robin.vowels@gmail.com>
Date2012-09-20 18:37 -0700
Message-ID<1113f6d2-94e6-4318-a181-191d5626416f@ph9g2000pbb.googlegroups.com>
In reply to#2226
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.

[toc] | [prev] | [next] | [standalone]


#2234

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-09-21 10:35 -0700
Message-ID<ez17s.128$l36.62@newsfe20.iad>
In reply to#2233
On 9/20/12 6:37 PM, Robin Vowels wrote:
> 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.
>
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.

[toc] | [prev] | [next] | [standalone]


#2235

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2012-09-22 22:48 +0200
Message-ID<k3l86n$f52$1@news.albasani.net>
In reply to#2226
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

[toc] | [prev] | [next] | [standalone]


#2236

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2012-09-22 23:14 +0200
Message-ID<k3l9na$jft$1@news.albasani.net>
In reply to#2235
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

[toc] | [prev] | [next] | [standalone]


#2237

FromPatricia Shanahan <pats@acm.org>
Date2012-09-22 16:12 -0700
Message-ID<6aWdneacAJZk2MPNnZ2dnUVZ_rydnZ2d@earthlink.com>
In reply to#2236
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.
>
> M. K. Shen
>

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

Patricia

[toc] | [prev] | [next] | [standalone]


#2238

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2012-09-23 15:25 +0200
Message-ID<k3n2ju$3n8$1@news.albasani.net>
In reply to#2237
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

[toc] | [prev] | [next] | [standalone]


#2263

FromRobin Vowels <robin.vowels@gmail.com>
Date2012-09-28 18:07 -0700
Message-ID<2783686a-5f40-4cfb-abd6-2d0f86952217@t2g2000pbt.googlegroups.com>
In reply to#2236
On Sep 23, 7:14 am, Mok-Kong Shen <mok-kong.s...@t-online.de> 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.

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.

[toc] | [prev] | [next] | [standalone]


#2260

FromFlorian Scholz <florian.scholz@stud.hn.de>
Date2012-09-28 18:36 +0200
Message-ID<k44jij$pv3$1@news.hs-niederrhein.de>
In reply to#2226
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.

[toc] | [prev] | [next] | [standalone]


#2261

Frombob <bob@coolfone.comze.com>
Date2012-09-28 15:05 -0700
Message-ID<cec59517-7a04-469c-898e-e9451c8aae9d@googlegroups.com>
In reply to#2260
On Friday, September 28, 2012 11:34:38 AM UTC-5, Florian Scholz wrote:
> 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.

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.

[toc] | [prev] | [next] | [standalone]


#2262

From"BartC" <bc@freeuk.com>
Date2012-09-28 23:39 +0100
Message-ID<k4590m$dhl$1@dont-email.me>
In reply to#2261
"bob" <bob@coolfone.comze.com> wrote in message 
news:cec59517-7a04-469c-898e-e9451c8aae9d@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
 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web