Adika Bintang Sulaeman
by Adika Bintang Sulaeman

Categories

Tags

Edsger Dijkstra said that “goto is considered harmful” [1].

However, there are some cases, at least in C programming, where goto is the clean way to do thing. In fact, there are a lot of goto in the Linux kernel source code [2] and Linus made some sound justification on this [3].

Suhu Erik [4] confirmed this goto usage and said that goto is a poor man’s exception handling: we can handle error without stack unwinding.

Consider this example:

void foo()
{
    int *x = malloc(sizeof(int));
    bool ok;

    // do one thing
    ok = do_one_thing();
    if (!ok)
        goto err_handling_point;
    
    // do one other thing
    ok = do_re_mi();
    if (!ok)
        goto err_handling_point;

err_handling_point:
    free(x);
}

Adding free for all if is lame. Forgetting calling free when returning from function is even more lame. This is one of the cleanest approach. I don’t know other approach in C. No, don’t talk RAII. We are not talking about C++11.