Re: regarding "goto" in C



On 2006-01-10, websnarf@xxxxxxxxx <websnarf@xxxxxxxxx> wrote:
> Christian Bau wrote:
>> websnarf@xxxxxxxxx wrote:
>> > Chris Dollin wrote:
>> > > websnarf@xxxxxxxxx wrote:
>> > > > I personally follow only one rule regarding goto: avoid using it unless
>> > > > using it leads to either clearer code or faster (executing) code. This
>> > > > rule alone is sufficient to make the use of goto fairly uncommon in my
>> > > > code to the point of "spaghetti code" never being an issue. (If
>> > > > there's one exception it would have to be parsers -- but in those cases
>> > > > even the cleanest control structures don't actually lead to a clearer
>> > > > representation of the parser.)
>> > >
>> > > I've never had to use a goto in a parser; could you unpack the reasons
>> > > why you've wanted one? [email if it's likely to be wildly off-topic.]
>> >
>> > Well, I measure the performance of the code I write, and I have never
>> > used/figured out yacc or bison or any of those tools. Most parsing
>> > that I implement boil down to what I would call state machines with
>> > counters. The gotos are for state transitions. The typical thing that
>> > I've seen is a switch statement wrapped in a while(), but as I
>> > mentioned switch is *extremely* slow as compared to a goto. Goto costs
>> > the CPU almost nothing (at worst 1-clock of empty decode) as compared
>> > to a break->continue->switch (essentially a goto, then a comparison, a
>> > branch, then an indirect branch) which you pay for in the "well
>> > structured" parsers.
>>
>> One suggestion for a C extension:
>>
>> continue <expr>;
>>
>> which can be used within a switch statement: It evaluates the
>> expression, then repeats the original switch statement with the value of
>> <expr> instead of the original value of the switch statement. Obviously
>> this can be optimised to a simple jump instruction if <expr> is a
>> constant expression. Would that get rid of your goto's?
>
> Yes it would -- I think we've discussed this here before, and I
> endorsed that or a similar change as well. But I can use goto and an
> extra label today ... so, the value of this may not be extremely high.

"goto case" would be possibly a more obvious way to implement the
functionality described.
.



Relevant Pages

  • Re: How to develop a random number generation device
    ... Assignments don't crash in assembly, ... think in state machines, so GOTO is perfectly logical. ... conditional branch, or a computed/table driven jump, are the primary ...
    (sci.electronics.design)
  • Re: switch() Statement Question
    ... not be executed within a switch statement, ... A break statement terminates execution of the smallest enclosing ... switch and found the goto. ... could stack an address for breaks to execute. ...
    (comp.lang.c)
  • Re: switch without breaks
    ... Why, though, are subsequent cases not evaluated? ... case labels are just that -- labels b) the numbers for the cases have to be compile time constants. ... a switch statement with breaks ...
    (comp.lang.c)
  • Re: [PATCH] Add a pair of system calls to make extended file stats available [ver #3]
    ... We use a macro in Lustre for compile-time assertions that depends on the detection of duplicate values in a switchstatement: ... Check an invariant described by a constant expression at compile time by ... expressions in the same switch statement shall have the same ... Can we add a couple more flags to make Samba's life easier? ...
    (Linux-Kernel)
  • Re: Horror! Using goto statement in finite state machines?
    ... the goto statement, accusing it of being some sort of spawn of satan. ... The simplest way to improve the C standard ... FSM can be implemented inside of a switch statement without paying the ... case BAR: BAR: ...
    (comp.lang.c)