Public Group

Use of goto in C++?

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Everyone always says that you shouldn't just always avoid goto, instead you should learn when it is appropriate and not appropriate to use. So what are the legitimate uses of goto? I can't think of any.

Share on other sites
while (whatever){  for (some; crap; here)  {     goto OutsideTheLoop;  }}OutsideTheLoop :

In C++, goto can be used to implement multi-level break.

Share on other sites
Use of Windows or Linux API which is C. Some of the idioms of these APIs are still simplest to express via goto, since bringing in either RAII or exceptions opens a whole different can of worms.

Share on other sites
while (whatever){  for (some; crap; here)  {     goto OutsideTheLoop;  }}OutsideTheLoop :

This is ugly. You should rather use:

while (!done){  for (some; crap; here)  {     done = true;     break;  }}

This will set done to true, and then break from the for loop, which in turn, will break the while loop. There is no need to use goto here.

Share on other sites
while (whatever && !done)

:)

Seriously, though: practically speaking, you don't have a reason to use goto. If you think you have a reason to use goto, show me the code and I'll give you a better idea.

(Often, the best solution for multi-level break is to refactor the loops into their own function and return from the function.)

Share on other sites
Lol thanks for correcting me. That is a better way of doing it if you also require another condition.

And I agree. There is no need for goto. I don't remember the last time I used it.

Share on other sites
Quote:
 Original post by KieranWwhile (whatever){ for (some; crap; here) { goto OutsideTheLoop; }}OutsideTheLoop :This is ugly. You should rather use:while (!done){ for (some; crap; here) { done = true; break; }}This will set done to true, and then break from the for loop, which in turn, will break the while loop. There is no need to use goto here.

The goto looks much cleaner and easier to read to me, especially if you have a reasonably named label. If there's a lot of code between the while and the for, or maybe even a third level, then you are gonna need to go back up, check what the done = true will do to the loop, and what if you have code after the for loop that you also wanna jump over?

The "evilness" of goto comes from jumping back and forth and all over the place, if all you're doing is jumping "down" over a bunch of code it is at least as clean as a break or a function call.

Share on other sites
Here we go again...

Share on other sites
The goto looks much cleaner and easier to read to me. If there's a lot of code between the while and the for, or maybe even a third level, then you are gonna need to go back up, check what the done = true will do to the loop, and what if you have code after the for loop that you also wanna jump over?[/QUOTE]

To everyone else, the other way looks cleaner.

It's obvious what done = true will do to the loop. It will break it because it's a while loop with the condition while(!done) which means as soon as done is set to true, the loop finishes. All the code after the for loop will not be executed once done = true, because the while loop will break as well and it will skip to the code after the loop. It essentially does the same thing as the goto did. If there is a third level, give that the condition of !done as well and it wall also break.

There is a solution to everything that is much cleaner than a goto.

Share on other sites
Quote:
 Original post by ZahlmanSeriously, though: practically speaking, you don't have a reason to use goto. If you think you have a reason to use goto, show me the code and I'll give you a better idea.

Example taken from MSDN, quickly fixed for in this editor, but hopefully it gets the picture across.
HBITMAP CopyBitmap( HBITMAP hbm) {    HBITMAP hbmOld, hbmOld2, hbmNew = 0;    BITMAP bm;        HDC hdcSrc = CreateCompatibleDC(NULL);    if (hdcSrc == NULL) goto errSrc;        HDC hdcDst = CreateCompatibleDC(NULL);    if (hdcDst == NULL) goto errDst;    GetObject(hbm, sizeof(bm), &bm);    hbmOld = SelectObject(hdcSrc, hbm);    if (hbmOld == NULL || hbmOld == GDI_ERROR) goto errOld;    hbmNew = CreateBitmap( bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel, NULL);    if (hbmNew == NULL) goto err;    hbmOld2 = SelectObject(hdcDst, hbmNew);    if (hbmOld2 == NULL || hbmOld == GDI_ERROR) goto err;    BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);err:    SelectObject(hdcSrc, hbmOld);errOld:    DeleteDC(hdcDst);errDst:    DeleteDC(hdcSrc);errSrc:    return hbmNew;}

Obviously there are other ways, and hopefully the WinAPI will make people cringe, and one could argue that it applies to C portion of C/C++ used for Windows development. Still...

Share on other sites

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

• 17
• 11
• 11
• 9
• 49
• Forum Statistics

• Total Topics
631394
• Total Posts
2999753
×