6

I know lots of questions about why not use goto, why goto is bad practice, why goto was create by devil, why the fingers of those who type goto should be ripped off, etc...

And in many answers to this questions , this question, and even at wikipedia it's possible to find reasons pro / against goto.

Even Stackoverflow has a goto tag :)

But, obviously, goto is accepted as a valid command, otherwise your compiler / interpreter wouldn't know what to do with it. Even in C / C++ it's possible to use goto, and that language even has a formal, ISO specification.

So, probably, there should be some case where only by using goto it's possible to accomplish some task. Then, why and when should a goto be used in C / C++ ?

Edit:

Just to make sure:

1 - this question doesn't require debate. It should be answerable: giving one or more cases where goto is the only solution, or stating no, it's always possible to avoid a goto. Of course, I'm not the only who can decide whether it should be closed or not, but I don't think that "too broad, asks for debate, etc" are reasons to close it.

2 - There might be some cases where goto make the code cleaner, faster, etc. I don't want those. I want to know if there exists one case where goto is the only solution (no, it's not a dup of that "give good examples of goto" question)

3 - I'll accept any downvote, with a smile on my face, if the downvote is justified. But I'm pretty sure that this question has an answer and has done previous research about the subject. Downvoting simple because those 4-letters taboo word that starts with a "G" was used... sorry...

Community
  • 1
  • 1
woliveirajr
  • 9,433
  • 1
  • 39
  • 49
  • 2
    This question is too opinionated for the Q/A format of Stack Overflow. The answer is mostly a matter of style, and depends very much on the exact situation. – Dave Aug 29 '13 at 18:40
  • 1
    no... I don't want an opinion. I want a concrete case where only goto could be used to solve some situation. I don't want opinions about speed, make-your-code-more-obscure... – woliveirajr Aug 29 '13 at 18:41
  • 2
    Then no. There is no case where `goto` is the *only* option. You can do any branching task using `if`, `for` and `switch`, but that can result in less readable code in some very rare cases. – Dave Aug 29 '13 at 18:41
  • @Dave would you mind making that an answer? – woliveirajr Aug 29 '13 at 18:42
  • Generated code sometimes needs goto. The rest can live without. –  Aug 29 '13 at 18:42
  • Any language that is Turing complete can do 'anything'. It's a matter of convenience and overall utility that 'extra' operations are added to a language. – Jiminion Aug 29 '13 at 18:44
  • C++: never, C: rarely – Paul R Aug 29 '13 at 18:45
  • @Hasturkun hum... that question shows many good examples where you can use a goto. But doesn't answer my existential doubt: is there a case where goto must be used, because it's the only solution ? – woliveirajr Aug 29 '13 at 18:46
  • @woliveirajr No, there isn't. `goto` might be better than something else in some situations, but it's never the only choice. – jrok Aug 29 '13 at 18:46
  • 1
    @woliveirajr - Sorry your question got marked as a dupe. It is NOT a dupe (or at least not a dupe of the one indicated). You're asking for a scenario where `goto` is the _only_ possible solution, which is a completely different question than where `goto` is a _good_ solution. – Andrew Cottrell Aug 29 '13 at 19:06
  • 3
    Regarding your updated comments, let's be honest: whatever the intent of the question, you are *going* to get mostly opinion-based answers, which is why I voted to close it as too broad (my vote got drawn in to the dupe votes but, nevermind). You have an answer from several sources now: No. There is no situation. The best you can hope for now is that every possibility is suggested and shot-down with counterexamples, which is not constructive. – Dave Aug 29 '13 at 19:07
  • @Dave I can expect that this questions gets one example (I doubt) or get a good answer showing some real examples where a code with goto can be changed to a code without goto, and then it's the answer... Agree with you, people can debate anything, of course, but it's not my question fault – woliveirajr Aug 29 '13 at 19:11

4 Answers4

10

There is no circumstance under which a goto is strictly necessary: you can always avoid it if you really want to, but to do that for purely idealogical reasons is a mistake.

For example, a friend of mine wrote a wavelet transform function that had (something like) 15 nested loops. In the event of an error in those loops, he had a goto to a cleanup block just before the function's return statement. To achieve the same effect without a goto would have involved setting a flag and testing it at every loop level, which would have made his code far less readable. In these circumstances, goto was the right choice.

Emmet
  • 6,192
  • 26
  • 39
  • 1
    Tell your friend that 15 nested loops is too many… by about 13. – Dave Aug 29 '13 at 18:53
  • 1
    Maybe that wasn't obvious to him 17 years ago. – Emmet Aug 29 '13 at 18:57
  • 1
    My point is that whether goto was the right choice in that situation or not is irrelevant: the situation itself was bad. Besides, this is an example of when `goto` might be the better option. The question is looking for cases where it is the *only* option. – Dave Aug 29 '13 at 19:11
  • 2
    Those cases don't exist, since the language is Turing complete without a `goto` construct. Therefore, the only reasonable answer, IMHO, is to state this fact and give an example of where it is preferable, even if it can be avoided, which is exactly what I did. If your point was related to the substantive issue in question, rather than a snipe at an irrelevant detail, your comment did not convey that very well. – Emmet Aug 29 '13 at 19:33
  • 1
    See my comments on the main question's comment thread if you want my full opinion. If you update your answer with the turing completeness argument (and I'd suggest removing the irrelevant example of it being preferable in a particular situation), I'd have no issue with it. – Dave Aug 29 '13 at 19:40
  • 3
    I'm happy with my answer as it is, thanks. – Emmet Aug 29 '13 at 19:41
6

The latest MISRA standard now allows gotos.

A good example where gotos make sense is when you have a large routine with a lot of exits points. You can have many return statements (not good) convolute the code with 'structured programming' conditionals (also not good) or a "goto Done; which sends the program to a set of ending statements before returning.

The MISRA standard basically allows gotos for these sort of circumstances. I think 'only downward' is one of their criteria.

Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • I fail to see how goto is better than multiple return statements. – jrok Aug 29 '13 at 18:43
  • 1
    http://stackoverflow.com/questions/17177276/c-c-conditional-return-statements/17177315#17177315 – Jiminion Aug 29 '13 at 18:47
  • 4
    @jrok: typically when you have a chunk of clean up code that would otherwise be duplicated if you used multiple returns. – Paul R Aug 29 '13 at 18:47
  • @PaulR Ok, I can imagine that in C. But in C++ RAII should be better for that, no? And when there's nothing to clean up, multiple return statements aren't neccesarily bad. – jrok Aug 29 '13 at 18:48
  • 2
    You invoke MISRA like it's some sort of bible that controls all programming. – Dan Aug 29 '13 at 18:53
  • There's a lot of things I don't like about MISRA. But it is a standard which tries to explain the rationale behind the decisions. I found it significant that they backed off banning gotos (and 'break', except for use in switch, and 'continue') compared with their earlier standard. MISRA is a C standard, so it's particular to C and not (I don't think) C++. – Jiminion Aug 29 '13 at 19:04
  • @jrok: yes, I was talking about C as the context here is MISRA. C++ is a different kettle of fish. – Paul R Aug 29 '13 at 19:43
5

The only reason I use a goto is when it is for an error return condition from a function that needs some common cleanup. The target of the goto is near the end of the function and the "normal" return returns before the label.

Charlie Burns
  • 6,994
  • 20
  • 29
  • 3
    Yes! If you have any final processing to do before returning, you can put that all in one place. A number of 'goto Done' s in the code is not confusing or spaghettish, as it is clear what is going on. – Jiminion Aug 29 '13 at 18:48
3

Here is an example where only goto will work: https://stackoverflow.com/a/245801/193848

Basically if you have multiple nested for loops, the only way to break out of all the loops from an inner loop is with a goto statement (since unlike some other languages the C break keyword doesn't support a parameter for the number of nesting levels to break out).

Community
  • 1
  • 1
Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41
  • 3
    Even this doesn't *require* goto. You can also use a variable as a marker that the loop is over, and break out of each level individually. `goto` simply provides a more readable and potentially faster (though I'd doubt it with modern compilers) option. Using a small function and `return` (as suggested in the comments on that answer) is also a good option which will compile to be almost identical to using a `goto`. – Dave Aug 29 '13 at 18:50
  • 2
    Yes, functionally that may work, but I think you missed the point. If you want to break out of all the loops _immediately_ then only `goto` will work. In your example you're adding a variable comparison check at every level of nesting, which isn't the same as breaking out immediately. – Andrew Cottrell Aug 29 '13 at 18:57
  • 1
    The second option (a separate function with a return) *will* break out immediately. I agree that in some cases a `goto` is more appropriate, but you cannot possibly claim it is the *only* way. – Dave Aug 29 '13 at 19:02
  • 1
    I am absolutely claiming it is the _only_ way to _immediately_ break out of nested `for` loops and continue with subsequent code in that function. I'm not saying that's the best practice and I'm not saying the loops etc couldn't be rewritten to accomplish the goal in another way. I'm saying it's the only way to accomplish that particular goal without rewriting the surrounding code. – Andrew Cottrell Aug 29 '13 at 19:13
  • Of course, by that standard you could argue that *all* uses of `goto` are cases "where only `goto` will work", because `goto` is the *only* way to *immediately* jump to an arbitrarily-specified place in the function. To remove `goto` from a function always involves rewriting the surrounding code. – ruakh Aug 30 '13 at 20:54