Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>C: anything by antirez (redis, etc...) Redis is the epitome of well written understandable C.

The first file I opened has 3 gotos but maybe it's pure randomness... ;p



gotos are sometimes used in C for error handling (to release resources), it's a simple way to do cleanup:

    void foo() {
        int* foo = malloc(...)
        if (foo == NULL) goto err_foo;
    
        int* bar = malloc(...)
        if (bar == NULL) goto err_bar;
    
        /* code */
    
    err_bar:
        free(bar);
    err_foo:
        free(foo);
    }
That's a use of goto that's often considered non-evil, probably because the alternatives are typically a lot more ugly.


Sorry to be a stickler, but the error handling in your method "frees" a resource even when nothing was allocated to it. Freeing a null pointer is as such an undefined behavior.


http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf, section 7.20.3.2 "The free function":

"The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs."


Dereferencing a null pointer is undefined behavior. free() on NULL is defined to be a no-op.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: