2

Possible Duplicates:
GOTO considered harmless
GOTO still considered harmful?

Is there any legitimate reason to ever use goto in a c++ program? Everyone i know says that the definition of goto is that youre programming something wrong, which i agree with, since i cant find any reason to use goto

Community
  • 1
  • 1
calccrypto
  • 8,583
  • 21
  • 68
  • 99

3 Answers3

11

goto is one elegant + efficient way to handle this:

for (...) {
   for (...) {
      for (...) {
          /* detect condition that requires breaking all three loops */

      }
   }
}
out:

Another example. Say you have a giant function - 2K lines. Don't be surprised, lots of networking code has this. And in the function, at various times, you detect conditions that require the same error handling: goto shines at this.

EDIT the link below has portions of the original article countering Dijkstra's article.

"GOTO statement considered harmful" considered harmful

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 1
    Feel free to replace any of those `for` loops with `if` statements as well. – Nicol Bolas Jun 25 '11 at 06:21
  • C++ doesn't have a labeled break? –  Jun 25 '11 at 06:27
  • `goto` never shines, but always stinks! – Gene Bushuyev Jun 25 '11 at 06:29
  • @pst I didn't know about labeled breaks. Can you please point me to an example ? – cnicutar Jun 25 '11 at 06:30
  • @cnicutar I guess not -- should have consulted the interwebs before. I don't use C++, but was sort of surprised to see it's not a support construct. –  Jun 25 '11 at 06:39
  • 3
    You could put all that code in a separate function and use a return instead. The return can only go to(!) one place, so we don't have to look for the label. – Bo Persson Jun 25 '11 at 06:48
  • @Bo Persson Pretty valid. I stand by my opinion though (plus I am not suffering the possible overhead of a function call). – cnicutar Jun 25 '11 at 06:51
  • @cnicutar you can inline the function after you prove that it incurs an overhead. – Tugrul Ates Jun 25 '11 at 06:56
  • @junjanes No, the compiler can inline it if it wants to. – cnicutar Jun 25 '11 at 06:57
  • 2
    @cnicutar - Do you think the compiler writers have spent large efforts on optimizing goto? The data flow analysis is a lot more difficult when there are gotos in the code. – Bo Persson Jun 25 '11 at 06:59
  • @Bo Persson: yes, they do, as most compilers lower all the ifs, loops and other things to gotos and all analyses and optimizations work on that. –  Jun 25 '11 at 10:08
  • C++ doesn't have labelled breaks, but if you have gotos, they are a redundant feature. (My ipad wanted to replace "gotos" with "Botox". What does it know? :-) – Ferruccio Jun 25 '11 at 11:16
4

When you need to break out of a deeply nested loop. Suppose you're searching for a value in a 3-dimensional matrix:

for( size_t i = 0; i != d0; ++i )
for( size_t j = 0; j != d1; ++j )
for( size_t k = 0; k != d2; ++k )
  if( m[i][j][k] == key )
  {
    // break out
  }

Of course you could always define a function and return out of it, but you may have one-time use cases in which writing a function (and thus moving the code out of context, carrying all the variables, and so on) is not worth the trouble just to avoid using goto.

Pablo
  • 8,644
  • 2
  • 39
  • 29
  • the only time when you need to get out of immediate context is in case of irrecoverable error, and that's what exceptions are for. And exceptions do it in safe way, allowing stack unwinding and proper destructor calls, `goto` commits violence, such code is absolutely terrible. – Gene Bushuyev Jun 25 '11 at 06:27
  • I meant that if you're doing something in a function, writing another one, and moving the loops (along with their required variables) into another function moves your code (and thus your intentions) out of the context they were in. Doing that just so you won't have to use `goto` is too much trouble. – Pablo Jun 25 '11 at 06:33
  • So I believe you understood my comment backward. I don't mean you *need* to get out of context, but rather that you *don't want* to. – Pablo Jun 25 '11 at 06:34
2

You could use goto for cleanups, something like:

void doSomething()
{
    if (someCondition)
        goto cleanUPA;
    if (otherCondition)
        goto cleanUPB;
    if (oneMoreCondition)
        goto cleanUPAll;

    //All good then just
    return;

cleanupUPB:
    //respective cleanups
cleanupUPA:
    //respective cleanups
cleanUPALL:
    //respective cleanups
}

Possibly, could be implemented in a better way by using RAII in C++ but coming from a C background this is often where goto is used, So if for any reason you cannot use RAII(I think hard to find such a scenario-possibly you are not using any smart pointers at all etc) then it can be a legitimate case.

Alok Save
  • 202,538
  • 53
  • 430
  • 533