last day (15 days later) » 

4:50 PM
7
A: How to deal with function exits on a function that has several exit points?

iharobYou can use goto for that. code = firstCode; if (condition != 0) goto label; code = secondCode; if (anotherCondition != 0) goto label; label: clean_up_code_if_necessary() exit(code); // may be you should return from the function but there could be many other options depending ...

 
I know that many will downvote this answer, but don't be irrational about goto, if goto wasn't useful, why it's still in the language?
 
because the latest C standard came on 98. people still used that at the time. also , because C is close to Assembly , it still contains many Assembly elements, that doesn't make them useful.
 
mhl
Actually the latest C standard is C11
 
@user3613500 you blindly believe that this code is wrong because of the use of goto but I have seen some of the best open source projects use this technique, and it's so clean and easy to follow that it would be a shame if the goto was removed from c.
 
dmg
@user2079303 The only reason it's not deprecated is compatibility. Code from the last century still uses goto.
 
4:50 PM
@dmg, sorry I removed my comment since I hadn't noticed that user3613500 claimed C98 to be the latest version which made my comment sound silly. But on the topic of my deleted comment, deprecation does not affect compatibility.
 
@dmg really??? are you sure?
 
This is perfectly fine and idiomatic error handling in C. Just be ready to move this answer if OP decides to switch this question to C++ ;)
 
@Quentin I am ready, if the OP picks C++ i will remove it... Also I am expecting more downvotes, I can't understand how "programmers" can believe in goto as a bad thing without reasoning about it...
@Quentin now the try/catch thing is not a good answer, the OP picked c.
 
@iharob Despite my earlier comment I upvoted your answer, this is probably the only situation I would say using goto is acceptable (so long as the label's name was a bit more descriptive). I think people just have a knee-jerk reaction to it since they either have had to use it for everything at some point in the past or who/whatever taught them to code railed against it and pointed out all the flaws.
 
4:50 PM
@iharob The C answer stands ! Well, I don't understand either. It's kind of a vicious circle where people are taught that goto is useless and dangerous, then repeat it to you and prove it by using it wrong. It's like banning scissors as lethal and proving your point by trying to remove one of your teeth with them.
 
@Namfuak yes I know, or they all read the title of Go To Statement Considered Harmful by Edsger Dijkstra, without even reading why.
 
@Quentin I was one of those persons taught not to use goto no matter what.
So using a goto cleanup; for example would be a better choice than the awkward do { ... break; ... break; ... } while (false); right?
 
@Claus I think it's easier to follow and read, and maintain too, but that is my opinion which might be wrong of course. Oh, BTW, good choice for the label name.
@Claus I see you are new to SO, did you take the tour?
 
@Claus well you want to jump. What do you use, the jump instruction, or a degenerate looping instruction, that doesn't actually loop because you want to bend it into a jump instruction ? The fact that you qualify the latter as "awkward" should be a hint.
 
@iharob Yes, I registered a couple of days ago and I took the tour at that time, read about the no discussion just questions and answers as well. But these days going through some sites this one still seemed the better one to make the question.
 
4:50 PM
In fact I believe that your question was really good, just that there are many answers to it. BTW, the do { ... } while (0) is useful for writing macros sometimes.
 
@iharob yes, but for another reason altogether (not screwing up the syntax if the macro spans several instructions).
 
@Quentin that's what I mean with sometimes...
 
You could use two labels to show how goto can help with several cleanups (something along close_file: if(fclose(file)) { ... }; free_mem: free(foo); return error_code;). That can't be achieved with breaking from a pseudo-loop. [edit: Or maybe not, another answer already did.]
 
@mafso, I can't because that's what mhl answer did. So let's see if the OP thinks that is more useful and picks that instead of putting that here after that answer appeared.
 

last day (15 days later) »