Hacker News new | ask | show | jobs
by faho 1429 days ago
First of all: Your code is missing braces around the `if` blocks - the `goto out` would be run unconditionally.

But anyway, the case for `goto` here is that it jumps immediately to the cleanup that needs to happen always.

If you put something between the loop and that, `break` would jump before that and also execute that.

Yes, this is a workaround for C's lack of automatic cleanup (RAII, garbage collection, python's `with` or whatever).

1 comments

> Your code is missing braces around the `if` blocks

Sure. It's obviously a sketch.

> If you put something between the loop and that, `break` would jump before that and also execute that.

Sure. But I rarely can see a need to make it so complicated (not in this case anyway). If you need multiple distinct cleanup sections that can't be inlined, then label them all (or put them in separate procedures) and jump to them explicitly. In my example, there is no need for any labels at all.

> this is a workaround for C's lack of automatic cleanup (RAII, garbage collection, python's `with` or whatever).

No need for workarounds here.