• ZILtoid1991@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      9 months ago

      I also use it for avoiding recursive function calls. In theory, this will tell the compiler to just ditch the current stack and go back to the beginning of the function.

    • bss03@infosec.pub
      link
      fedilink
      English
      arrow-up
      2
      ·
      9 months ago

      Java doesn’t allow goto, but specifically does have labels for labeled break/continue to support the multi-loop exiting case.

      I imagine these two “structures” will always be implemented in C source through disciplined use of goto.

      • Gladaed@feddit.org
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        9 months ago

        It’s literally the only way to do this. Other ways include checking of loads of bools. That’s slow.

        • bss03@infosec.pub
          link
          fedilink
          English
          arrow-up
          1
          ·
          9 months ago

          In C maybe. In language that support proper recursion schemes, the apomorphism models the early-exit loop.

  • Frezik@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    8
    ·
    9 months ago

    IIRC, this is because gcc optimizes goto very well, or at least it did back in the day. It also is a genuinely workable solution for error handling in C.

    Consider if you need to setup three things, do something with them, and then tear them down in reverse order. If there’s an error on the second thing, you want to jump right to the part where you tear down the first thing. Using goto tends to make cleaner code for that in C compared to, say, nested conditionals.

  • stormeuh@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    9 months ago

    Seems like it’s mostly error handling, which makes total sense to me. In a function with a lot of error conditions, where it also takes more than return <nonzero value> to report that error, the code would get very cluttered if you handle the errors inline. Using goto in that case makes the normal case shorter and more readable, and if proper labels are used, it also becomes clear what happens in each error case.

    Sure, you can do that with functions too, but it’s much nicer staying in the same scope where the error occurred when reporting on it. Putting things in a function means thinking about what to pass, and presents extra resistance when you want to report extra info, because you have to change the function signature, etc.

    • squaresinger@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      9 months ago

      Not really. There are quite a few of structures you can make with gotos that can’t directly be translated into functions. Sure, you can implement any functionality in any programming paradigm, but it might require much more work than to just replace goto with a function call.