Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Just curious, not a C developer by any means, but why wouldn't you use a function here instead of a goto? I'm confused how goto would reduce error/improve readability in that example.

Again, not criticizing, genuinely want to know.



sort by: page size:

And goto being used for error handling is pretty stock standard across most C codebases I’ve worked on or seen over the years, so I’m not sure what the particular gripe is there

misunderstanding goto is cargo cult programming.

loops and functions are compiled down to goto (or jump). Everyone uses them behind higher level structures.

pure goto is indispensably useful for error handling in C.


Really? Not trying to be derisive, just surprised. One of the common paradigms in C is GOTO for cleanup and error handling. I know it can be avoided if you really try however when used correctly, it can greatly improve the readability of code.

I agree in the general case but there are many cases where a goto statement is actually more readable than the alternatives in C. Especially if you choose descriptive labels.

As mentioned above, in C goto is often useful for error handling (where you need to free some resources before exiting the function). It's way more convenient and readable to write freeing code once and jump to it instead of having multiple return exits and duplicate the code before every single one of them.

I am not very familiar with low level C or the coding patterns used with it, so I must ask; why do we see goto statements in both of these recent bugs? As far as I know it's a bad practice overall, is there some specific reason for using it here?

Why wouldn't you use goto? It's not like it isn't constrained to the function you're in.

I was under the impression that the addition of "goto" was a joke. Are people actually using it? While there are legitimate uses for "goto," they are pretty much isolated to low-level C code, and are made unnecessary in a language that supports exceptions...

That’s what I am thinking of. These days people use goto only to reduce spaghetti code in very specific use cases in C and nothing more.

This thread isn't about safety though. It's about readability (spaghetti code). IMHO gotos in C when used for for dealing with errors at the end of the function make the code clearer.

Well if you're using C, you could just use a proper goto.

In this case goto is used as a form of "exception" handling, which is a perfectly fine use case for goto if you have an imperative language like C without exceptions or exception handling mechanisms. In fact it would probably be bad style not to use goto in this case.

If you have a better way to express the control flow you should use it instead though.


Using goto is standard for handling errors in C. It's used pervasively in the Linux kernel.

Don't really see a problem using goto there. Simplyfies control flow and error handling. Assuming we are talking about same piece of code.

The use of goto in C for error and exception handling is good practice. It keeps the code easy to read, and also provides common code for error and/or exit handling. You'll see this paradigm used a lot in large open source C projects, such as the Linux kernel and QEMU.

In my experience, a lot of closed source C projects ban goto outright, in (IMO) an overly dogmatic adherence to the idea that all goto use is spaghetti code and therefore bad.


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 ?

I don't agree `goto` is all bad. Whilst the OP said there are exceptions, they didn't list them. An ideal use (in C) is to ensure that in an error state, resources are cleaned up properly by jumping to error handling code (within the same function).

Goto is actually relatively common in C code. As in this case, it's usually used for error handling in much the same manner as exceptions might be. The document "/usr/src/linux/Documentation/CodingStyle" actually recommends such usage.

The goto's are for proper cleanup on error.

All of the options for doing so in C are awful; I just think goto is the least bad option. Otherwise, you get if statements that keep nesting, deeper and deeper.

And what's the hacky-ish logic you're talking about?

next

Legal | privacy