UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Using the goto statement, bad practice ?

#1 2012-01-23 03:02 PM

RipRage
Member
From: England
Registered: 2010-01-06
Posts: 146

Using the goto statement, bad practice ?

Hi All!

I would like to know your thoughts on the 'goto' statement, is it bad practice to use it? if not, what scenario should it be used in?

An Example:

Some code I have written recently loops through multiple linked lists looking for certain information, once that information is found it is then assigned to another linked list. Now in this scenario, because many for() statements are used, and we may need to go down 5-7 loops deep, functions like malloc(), inet_ntop() may be used, if these functions fail is it then acceptable to use the 'goto' statement to divert to clean up and exit failure?

Looking forward to your reply.

Offline

#2 2012-01-23 08:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Using the goto statement, bad practice ?

Offline

#3 2012-01-24 12:35 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Using the goto statement, bad practice ?

Yeah, I agree with Rob here. A goto is sometimes the cleanest
way to handle error paths, especially if you can replace multiple
error paths with one error path with multiple labels. Imagine
doing two mallocs. If the second one fails you have to free the
first allocation. If other things can go wrong too then goto's
can make the code a lot clearer and more robust (less chance
of forgetting to do anything in an error path, and all error
code is in one place). This seems to be your case.

Another case where a goto is the cleanest way out is in some
situations with switch statements in a loop, where break would
get you out of the switch but not out of the loop.

And sometimes, very sometimes, you have this tricky function
with a rare "retry" case, and that retry case is pretty much the
same, but jumped from a different position within the function.
A goto is fine too then.

But a goto won't fix too deeply nested function problems, those
require just helper functions and things like that.

The hate against goto came from the time when they could jump
around anywhere in the code, in C they're limited to one function,
so they aren't as evil as they used to be.

Offline

#4 2012-01-24 02:49 PM

RipRage
Member
From: England
Registered: 2010-01-06
Posts: 146

Re: Using the goto statement, bad practice ?

Interesting, Thank you both for your reply's.

Offline

#5 2012-01-24 02:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Using the goto statement, bad practice ?

Offline

#6 2012-06-06 12:18 PM

Dmitry
Guest

Re: Using the goto statement, bad practice ?

  • Index
  • » C
  • » Using the goto statement, bad practice ?

Board footer

Powered by FluxBB