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


Groups > comp.lang.c > #156861 > unrolled thread

Effective uses of c `goto' statement

Started bykevin shell <kevin@fedora.osfans.org>
First post2020-12-02 16:42 +0800
Last post2020-12-11 19:27 -0800
Articles 14 on this page of 114 — 24 participants

Back to article view | Back to comp.lang.c


Contents

  Effective uses of c `goto' statement kevin shell <kevin@fedora.osfans.org> - 2020-12-02 16:42 +0800
    Re: Effective uses of c `goto' statement Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-02 01:07 -0800
    Re: Effective uses of c `goto' statement Anton Shepelev <anton.txt@gmail.com> - 2020-12-02 20:18 +0300
      Re: Effective uses of c `goto' statement Anton Shepelev <anton.txt@gmail.com> - 2020-12-02 20:56 +0300
      Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-02 19:30 +0100
        Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-02 19:35 +0000
          Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-02 21:05 +0100
    Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-02 18:07 +0000
      Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-02 19:33 +0100
    Re: Effective uses of c `goto' statement Siri Cruise <chine.bleu@yahoo.com> - 2020-12-02 10:22 -0800
      Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-02 20:18 +0000
        Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-02 23:16 +0100
          Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-03 01:31 +0000
            Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-03 08:39 +0100
              Re: Effective uses of c `goto' statement Siri Cruise <chine.bleu@yahoo.com> - 2020-12-02 23:48 -0800
              Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-03 17:49 +0000
            Re: Effective uses of c `goto' statement Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-03 17:09 +0100
        Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-05 08:28 -0800
          Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-05 12:41 -0500
            Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 05:03 -0800
              Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-06 08:51 -0500
                Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-06 08:56 -0500
                Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 18:43 -0800
              Re: Effective uses of c `goto' statement Öö Tiib <ootiib@hot.ee> - 2020-12-06 05:58 -0800
                Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 16:54 -0800
          Re: Effective uses of c `goto' statement Siri Cruise <chine.bleu@yahoo.com> - 2020-12-05 10:01 -0800
            Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 04:55 -0800
      Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-03 21:20 -0800
        Re: Effective uses of c `goto' statement Siri Cruise <chine.bleu@yahoo.com> - 2020-12-03 21:50 -0800
          Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-03 23:06 -0800
            Re: Effective uses of c `goto' statement Siri Cruise <chine.bleu@yahoo.com> - 2020-12-04 00:27 -0800
    Re: Effective uses of c `goto' statement foo <opaquefoo@gmail.com> - 2020-12-02 11:03 -0800
      Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-02 21:02 +0100
        Re: Effective uses of c `goto' statement Öö Tiib <ootiib@hot.ee> - 2020-12-05 03:04 -0800
          Re: Effective uses of c `goto' statement Öö Tiib <ootiib@hot.ee> - 2020-12-05 03:36 -0800
            Good thing this isn't a Python group (Was: Effective uses of c `goto' statement) gazelle@shell.xmission.com (Kenny McCormack) - 2020-12-05 11:45 +0000
              Re: Good thing this isn't a Python group (Was: Effective uses of c `goto' statement) David Brown <david.brown@hesbynett.no> - 2020-12-05 14:38 +0100
                Re: Good thing this isn't a Python group (Was: Effective uses of c `goto' statement) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-12-06 11:00 +0000
                  Re: Good thing this isn't a Python group (Was: Effective uses of c `goto' statement) gazelle@shell.xmission.com (Kenny McCormack) - 2020-12-06 12:33 +0000
                  Re: Good thing this isn't a Python group Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-06 14:09 -0800
                    Re: Good thing this isn't a Python group Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-12-08 21:01 +0000
            Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-05 14:37 +0100
    Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-02 20:07 +0000
    Re: Effective uses of c `goto' statement Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-03 17:10 +0100
    Re: Effective uses of c `goto' statement "jfbod...@gmail.com" <jfbode1029@gmail.com> - 2020-12-03 15:40 -0800
      Re: Effective uses of c `goto' statement Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-03 16:14 -0800
      Re: Effective uses of c `goto' statement kevin shell <kevin@fedora.osfans.org> - 2020-12-04 16:29 +0800
        Re: Effective uses of c `goto' statement "jfbod...@gmail.com" <jfbode1029@gmail.com> - 2020-12-04 07:09 -0800
        Re: Effective uses of c `goto' statement "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2020-12-04 10:14 -0800
      Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-04 11:19 +0000
        Re: Effective uses of c `goto' statement Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-04 04:35 -0800
          Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-04 13:50 +0000
            Re: Effective uses of c `goto' statement Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-04 10:49 -0800
        Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-04 13:09 +0000
          Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-04 14:03 +0000
    Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-03 23:02 -0800
      Re: Effective uses of c `goto' statement Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-04 19:38 +0100
        Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-05 01:29 -0800
      Re: Effective uses of c `goto' statement kegs@provalid.com (Kent Dickey) - 2020-12-09 00:23 -0600
        Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-09 12:39 +0000
        Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-10 10:45 -0800
    Re: Effective uses of c `goto' statement Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-04 19:35 +0100
    Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-04 19:49 -0500
      Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-04 20:52 -0500
        Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-05 02:19 +0000
          Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-05 01:43 -0800
          Re: Effective uses of c `goto' statement "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-12-05 02:00 -0800
            Re: Effective uses of c `goto' statement "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-12-05 02:01 -0800
            Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-05 22:45 +0000
              Re: Effective uses of c `goto' statement "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-12-11 19:33 -0800
                Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-12 21:12 +0000
          Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-05 12:49 +0000
            Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-05 22:42 +0000
              Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 05:46 -0800
              Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-06 14:16 +0000
                Re: Effective uses of c `goto' statement Öö Tiib <ootiib@hot.ee> - 2020-12-06 06:54 -0800
                  Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-06 16:19 +0000
                    Re: Effective uses of c `goto' statement Öö Tiib <ootiib@hot.ee> - 2020-12-06 15:34 -0800
                      Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-06 23:56 +0000
                        Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 11:11 +0100
                          Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-07 11:41 +0000
                            Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 15:35 +0100
                              Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-07 15:48 +0000
                            Re: Effective uses of c `goto' statement Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-07 11:46 -0800
                              Re: Effective uses of c `goto' statement Bart <bc@freeuk.com> - 2020-12-07 20:01 +0000
                                Re: Effective uses of c `goto' statement Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-07 13:03 -0800
                      Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 11:01 +0100
                        Re: Effective uses of c `goto' statement Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-07 03:35 -0800
                          Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-07 07:33 -0500
                            Re: Effective uses of c `goto' statement gazelle@shell.xmission.com (Kenny McCormack) - 2020-12-07 13:11 +0000
                            Re: Effective uses of c `goto' statement Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-07 05:24 -0800
                              Re: Effective uses of c `goto' statement Richard Damon <Richard@Damon-Family.org> - 2020-12-07 08:58 -0500
                                Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 15:48 +0100
                              Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 15:45 +0100
                                Re: Effective uses of c `goto' statement Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-12-07 07:29 -0800
                            Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-07 22:34 +0000
                          Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 15:38 +0100
            Re: Effective uses of c `goto' statement kegs@provalid.com (Kent Dickey) - 2020-12-10 15:45 -0600
          Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-07 21:43 +0000
            Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-07 23:02 +0000
              Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-10 21:15 +0000
                Re: Effective uses of c `goto' statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-10 22:48 +0000
                  Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-11 20:59 +0000
                    Re: Effective uses of c `goto' statement Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-11 14:07 -0800
        Re: Effective uses of c `goto' statement Bonita Montero <Bonita.Montero@gmail.com> - 2020-12-05 08:55 +0100
        Re: Effective uses of c `goto' statement Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-05 02:04 -0800
        Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-07 21:37 +0000
          Re: Effective uses of c `goto' statement James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-12-07 17:03 -0500
            Re: Effective uses of c `goto' statement David Brown <david.brown@hesbynett.no> - 2020-12-07 23:17 +0100
          Re: Effective uses of c `goto' statement Kaz Kylheku <563-365-8930@kylheku.com> - 2020-12-07 22:19 +0000
            Re: Effective uses of c `goto' statement Vir Campestris <vir.campestris@invalid.invalid> - 2020-12-10 21:13 +0000
        Re: Effective uses of c `goto' statement kegs@provalid.com (Kent Dickey) - 2020-12-10 15:30 -0600
    Re: Effective uses of c `goto' statement "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2020-12-05 13:01 -0500
    Re: Effective uses of c `goto' statement luser droog <luser.droog@gmail.com> - 2020-12-11 19:27 -0800

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#157204

FromVir Campestris <vir.campestris@invalid.invalid>
Date2020-12-10 21:15 +0000
Message-ID<rqu352$se6$2@dont-email.me>
In reply to#157058
On 07/12/2020 23:02, Ben Bacarisse wrote:
> These rather generic examples make me wary.  What are these lines of
> code, and why are they not part of an initialisation function?  I'd
> really like to see an actual example.

One I've met is reading data from a config file.

Your initialisation has to:
- Open the file
- Read through it until it finds the correct data, probably storing each 
line in a temporary buffer
- Allocate a buffer to hold the data, and copy it in
- Close the file.
- free the temporary buffer.

Andy

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


#157209

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-12-10 22:48 +0000
Message-ID<878sa59t0q.fsf@bsb.me.uk>
In reply to#157204
Vir Campestris <vir.campestris@invalid.invalid> writes:

> On 07/12/2020 23:02, Ben Bacarisse wrote:
>> These rather generic examples make me wary.  What are these lines of
>> code, and why are they not part of an initialisation function?  I'd
>> really like to see an actual example.
>
> One I've met is reading data from a config file.
>
> Your initialisation has to:
> - Open the file
> - Read through it until it finds the correct data, probably storing
> each line in a temporary buffer
> - Allocate a buffer to hold the data, and copy it in
> - Close the file.
> - free the temporary buffer.

I' hoped to see code.  I can't see any obvious reason why this would be
better off written using the "jump to cleanup" pattern.

-- 
Ben.

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


#157216

FromVir Campestris <vir.campestris@invalid.invalid>
Date2020-12-11 20:59 +0000
Message-ID<rr0mj2$juk$1@dont-email.me>
In reply to#157209
On 10/12/2020 22:48, Ben Bacarisse wrote:
> I' hoped to see code.  I can't see any obvious reason why this would be
> better off written using the "jump to cleanup" pattern.

You may be right. I'd have to rummage for a real example.

The only place I know where it's easy to find goto is in Linux kernel 
and drivers, where they always follow the goto cleanup pattern.

Andy

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


#157217

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2020-12-11 14:07 -0800
Message-ID<871rfwdmj3.fsf@nosuchdomain.example.com>
In reply to#157216
Vir Campestris <vir.campestris@invalid.invalid> writes:
> On 10/12/2020 22:48, Ben Bacarisse wrote:
>> I' hoped to see code.  I can't see any obvious reason why this would be
>> better off written using the "jump to cleanup" pattern.
>
> You may be right. I'd have to rummage for a real example.
>
> The only place I know where it's easy to find goto is in Linux kernel
> and drivers, where they always follow the goto cleanup pattern.

Just looking at some projects for which I have sources, most of the
large ones have a number of gotos (typically hundreds) in C sources.
That includes gcc, glibc, emacs, vim, perl5, cpython, and git.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

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


#156930

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-12-05 08:55 +0100
Message-ID<rqfed1$nqj$2@dont-email.me>
In reply to#156926
>> When using the goto to jump to the resource cleanup at the end of the
>> function, changing those to ifs adds a level for each resource that
>> needs to be gotten. If you have 4 resources, at an indent of 4, is 16
>> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>> the function uses a couple more, that can be excessive.

>    You seem to have ignored all of what I wrote,
>    otherwise, by now, you'd know better.

Having nested functions for this instead of gotos is even less readable.
And notice you have to hand over the outer functions variables as poin-
ters; thats a mess. So goto here is a much cleaner solution.
I notice again that you aren't a practitioner, but a theorist which
does not think much.

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


#156935

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2020-12-05 02:04 -0800
Message-ID<861rg4muvw.fsf@linuxsc.com>
In reply to#156926
ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Richard Damon <Richard@Damon-Family.org> writes:
>
>> When using the goto to jump to the resource cleanup at the end of
>> the function, changing those to ifs adds a level for each resource
>> that needs to be gotten.  If you have 4 resources, at an indent of
>> 4, is 16 characters, or 25% of the 'typical' 80 character line.  If
>> the 'meat' of the function uses a couple more, that can be
>> excessive.
>
>   You seem to have ignored all of what I wrote,
>   otherwise, by now, you'd know better.

Hmmm, let's see...  Perusing this subthread, I see Richard Damon
raising specific points in support of using goto for this one use
case;  in response I see Stefan Ram giving vague generalities and
then simply making an ad hominem remark.  I wonder why anyone
would think the latter offers a more convincing viewpoint.

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


#157047

FromVir Campestris <vir.campestris@invalid.invalid>
Date2020-12-07 21:37 +0000
Message-ID<rqm7be$k2u$2@dont-email.me>
In reply to#156926
On 05/12/2020 01:52, Richard Damon wrote:
> If you have 4 resources, at an indent of 4, is 16
> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
> the function uses a couple more, that can be excessive.

I was using 80 character lines back in the 1970s. Things have moved on a 
little... (I just measured a window at 190 chars)

Andy

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


#157052

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2020-12-07 17:03 -0500
Message-ID<rqm8rn$ph$1@dont-email.me>
In reply to#157047
On 12/7/20 4:37 PM, Vir Campestris wrote:
> On 05/12/2020 01:52, Richard Damon wrote:
>> If you have 4 resources, at an indent of 4, is 16
>> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>> the function uses a couple more, that can be excessive.
> 
> I was using 80 character lines back in the 1970s. Things have moved on a 
> little... (I just measured a window at 190 chars)

It's not always a good idea to use the whole width available. Going from
the end of a long line to the start of the next line is hard on your
eyes, if you have to do it frequently.

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


#157054

FromDavid Brown <david.brown@hesbynett.no>
Date2020-12-07 23:17 +0100
Message-ID<rqm9m3$826$1@dont-email.me>
In reply to#157052
On 07/12/2020 23:03, James Kuyper wrote:
> On 12/7/20 4:37 PM, Vir Campestris wrote:
>> On 05/12/2020 01:52, Richard Damon wrote:
>>> If you have 4 resources, at an indent of 4, is 16
>>> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>>> the function uses a couple more, that can be excessive.
>>
>> I was using 80 character lines back in the 1970s. Things have moved on a 
>> little... (I just measured a window at 190 chars)
> 
> It's not always a good idea to use the whole width available. Going from
> the end of a long line to the start of the next line is hard on your
> eyes, if you have to do it frequently.
> 

That is certainly the case for dense text - more than about 12 words per
line (of English) reduces readability.  Code usually has shorter lines,
and a fair proportion of them will be indented significantly.  In this
case, a few longer lines is not going to be a problem unless they are
/very/ long.  But if you have several long lines in a row, it can make
sense to reduce the length or add some vertical space.

To me, the real reason to limit line length is that I normally want more
than one coding window on the screen at a time.  I generally like three
windows horizontally if they are "naked", or two if there is other IDE
information (lists of files, outline of functions/types/variables, etc.)
on the screen.  I find about 120 character lines works fine for that
with a size I find comfortable on the screens I use.  This gives a lot
more freedom than 80 characters - then it's rare that I have to split
something that would naturally be a single line.

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


#157055

FromKaz Kylheku <563-365-8930@kylheku.com>
Date2020-12-07 22:19 +0000
Message-ID<20201207141900.795@kylheku.com>
In reply to#157047
On 2020-12-07, Vir Campestris <vir.campestris@invalid.invalid> wrote:
> On 05/12/2020 01:52, Richard Damon wrote:
>> If you have 4 resources, at an indent of 4, is 16
>> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>> the function uses a couple more, that can be excessive.
>
> I was using 80 character lines back in the 1970s. Things have moved on a 
> little... (I just measured a window at 190 chars)

Wouldn't it be more useful to have two 100 column windows side by side?

-- 
TXR Programming Language: http://nongnu.org/txr

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


#157203

FromVir Campestris <vir.campestris@invalid.invalid>
Date2020-12-10 21:13 +0000
Message-ID<rqu31c$se6$1@dont-email.me>
In reply to#157055
On 07/12/2020 22:19, Kaz Kylheku wrote:
> On 2020-12-07, Vir Campestris <vir.campestris@invalid.invalid> wrote:
>> On 05/12/2020 01:52, Richard Damon wrote:
>>> If you have 4 resources, at an indent of 4, is 16
>>> characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>>> the function uses a couple more, that can be excessive.
>>
>> I was using 80 character lines back in the 1970s. Things have moved on a
>> little... (I just measured a window at 190 chars)
> 
> Wouldn't it be more useful to have two 100 column windows side by side?
> 
One of the advantages to working on TVs is that my main monitor is a 
50-inch 4k screen. It will take _four_ 100 column windows... :)

David's 120 seems reasonable. Besides which a standard line printer is 
132 columns, so it fits that.

(what's that? Oh, no I haven't used one of them since before the millennium)

Andy

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


#157205

Fromkegs@provalid.com (Kent Dickey)
Date2020-12-10 15:30 -0600
Message-ID<5dydndliwqXDDk_CnZ2dnUU7-YPNnZ2d@giganews.com>
In reply to#156926
In article <sJByH.38188$vhX.5591@fx17.iad>,
Richard Damon  <Richard@Damon-Family.org> wrote:
>On 12/4/20 8:15 PM, Stefan Ram wrote:
>>   When would I use "goto"? When I have to translate a piece of
>>   code from some other language to C or have to integrate a
>>   piece of C code and do not have the time to carefully remove
>>   the "goto"s, I'd rather leave the "goto"s in the code than
>>   to introduce bugs by hastily trying to remove them.
>> 
>
>When using the goto to jump to the resource cleanup at the end of the
>function, changing those to ifs adds a level for each resource that
>needs to be gotten. If you have 4 resources, at an indent of 4, is 16
>characters, or 25% of the 'typical' 80 character line. If the 'meat' of
>the function uses a couple more, that can be excessive.
>
>It also means that adding or removing a resource for the operation
>(moving a buffer from an array on the stack to being malloced) now
>changes the indent of the whole program, which plays badly with many
>source control systems.
>
>Yes, there are 'tricks' that can undo most of that extra indentation, by
>adding state variables, and a lot of extra ifs, but they often make the
>code look less clean.

I already posted in this thread an alternative to handling resource cleanup
which is actually less code (it pushes complexity off to another function),
adds no indentation or complex state variables, and actually improves the
code by adding additional deadlock checks (and provides an important
debug resource to print out in case of errors).  To summarize, active
resource state is kept in an a structure (which has an array of allocations),
and the calling routine calls the undo_alloc() routine afterwards.

And this is just the first thought that occurred to me.  I get that Linux
has gone the goto route, but that was a choice, and other choices are
possible.

I've found most code using goto to be very difficult to follow and understand,
especially if the goto target is not within "one screen" of the goto.
Very simple forward jumps are easy to follow, but most code using goto
isn't simple.  Goto makes it harder to "browse" code.

Goto is VERY handy for automated code generators to use.  But that's not
human readable.

Kent

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


#156955

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2020-12-05 13:01 -0500
Message-ID<rqghtt$mi2$1@dont-email.me>
In reply to#156861
On 12/2/20 3:42 AM, kevin shell wrote:
> Hello C hackers/masters. :-)
> 
>    A lot of C textbooks say not to use `goto' statement,
> but the fact is I find lots of Unix/Linux
> C code often use `goto' statement.
> 
>    My question is how to effectively use `goto' statement with C code,
> how to use `goto' to jump forward and jump back,
> how to avoid multiple `goto' statements with both jump forward and jump
> backward in the same function from messing up?

Hi Kevin.

My general rule of thumb for goto statements are these:

     (1) Whenever it makes the code clearer by simplifying the
         alternate structured way of creating the same logic flow,
         use a goto.

     (2) Whenever you have a common target point of code you want
         to use, but it's not always accessible through structured
         and nested code in an easy / straight-forward manner.

Gotos are most desirable, but they should be used sparingly because they 
can produce code that's spaghetti code.  If used intelligently, they can 
add beautify, form, and function to your code.

-- 
Rick C. Hodgin

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


#157218

Fromluser droog <luser.droog@gmail.com>
Date2020-12-11 19:27 -0800
Message-ID<273db341-8b86-4214-9458-8c4c15520c75n@googlegroups.com>
In reply to#156861
On Wednesday, December 2, 2020 at 2:43:13 AM UTC-6, kevin shell wrote:
> Hello C hackers/masters. :-) 
> 
> A lot of C textbooks say not to use `goto' statement, 
> but the fact is I find lots of Unix/Linux 
> C code often use `goto' statement. 
> 
> My question is how to effectively use `goto' statement with C code, 
> how to use `goto' to jump forward and jump back, 
> how to avoid multiple `goto' statements with both jump forward and jump 
> backward in the same function from messing up? 
> 

tldr; Goto is the most primitive tool from a larger toolset, and should be
treated as such.

I'll copy an answer I posted to
https://codereview.stackexchange.com/questions/250656/a-small-goto-text-adventure-game/250695#250695


paladin (OP):
> May someone teach me some hidden tricks in using GOTO?

`goto` is kind of a one trick pony. But is part of a family of control transfer constructs. All the looping and decision structures are in a sense a refined or specialized application of a `goto`. So from that pov, using `goto` is advisable only if it cannot be done with any of the "normal" control structures like `if`, `while`, `for` etc.

The next level up is function calls. A function call is a super-powered `goto`. Not only can you jump and execute a named piece of code, but you can also jump back right where you came from. Plus you can pass arguments and return a value, if desired.

Another level up is making use of function pointers. A pointer to a function can be saved in a variable or an array. It can be passed to and returned from functions. For a state machine like in the question, I'd be very tempted to organize the rooms into an array of function pointers. But I'd probably use a lot of macros, too.

The next level up from functions is setjmp/longjmp. These let you jump back across several levels of the call stack. It's sometimes useful to have a setjmp call in the main loop or initialization of the program and then the program can restart or bail-out if it runs into certain recoverable errors.

I suppose the next level up might be signal handlers and/or forking off child processes. Or maybe loading a dynamic library.

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

Back to top | Article view | comp.lang.c


csiph-web