3 ms·
I have no knowledge of C, but reading their code I was surprised to see some gotos ... is this OK in C to use goto ?
by jmnicolas 11y ago
I have no knowledge of C, but reading their code I was surprised to see some gotos ... is this OK in C to use goto ?
- mborch 11y agoYes.
- masklinn 11y agoIt's a very common C error-handling pattern, essentially emulating RAII with forwards-only function-local goto statements. It's possible to do without, but that tends to yield much more deeply nested code (especially if it's complex) and possibly some amount of duplication. See http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c http://eli.thegreenplace.net/2009/04/27/using-goto-for-error... for a short rundown, or http://blog.regehr.org/archives/894 http://blog.regehr.org/archives/894 where regehr — who works specifically in correctness and verifiability — states: > many people, including me, prefer the goto version
- deleted 11y ago[deleted]
- xorcist 11y agoMost languages use unconditional jumps to break flow when necessary, it might be called "exception" instead of "goto" but it's conceptually the same thing.
- AUmrysh 11y agoIn many high level languages a break, continue, pass, that sort of thing (where you want to skip or escape a loop iteration) is usually implemented with a goto as well. Also, a common use of goto in C is when you're doing something in nested for loops and want to break out of all of them. A goto is actually much cleaner here than using sentinel values or boolean flags, or naughty things like setting your loop variables to be outside of the conditional bounds.
- masklinn 11y agoWell if you go that way, fundamentally all structured programming techniques are implemented with a goto, that's what jumps are.
- dtech 11y agoC lacks exception handling. goto is used to implement it. Every conditional is a goto (jump) at the machine level.