Error handling in modern C

1 day ago 3

Hi guys, I'm not exactly a newcomer in C, quite the opposite in fact. I learned C about 25 years ago at a very old-fashioned company. There, I was taught that using gotos was always a bad idea, so they completely banned them. Since then, I've moved on to other languages and haven't written anything professional in C in about 15 years. Now I'm trying to learn modern C, not just the new standards, but also the new ways of writting code. In my journey, I have found that nowadays it seems to be common practice to do something like this for error handling:

int funcion(void) { FILE *f = NULL; char *buf = NULL; int rc = -1; f = fopen("file.txt", "r"); if (!f) goto cleanup; buf = malloc(1024); if (!buf) goto cleanup; rc = 0; cleanup: if (buf) free(buf); if (f) fclose(f); return rc; }

Until now, the only two ways I knew to free resources in C were with huge nested blocks (which made the code difficult to read) or with blocks that freed everything above if there was an error (which led to duplicate code and was prone to oversights).

Despite my initial reluctance, this new way of using gotos seems to me to be a very elegant way of doing it. Do you have any thoughts on this? Do you think it's good practice?

submitted by /u/siete82 to r/C_Programming
[link] [comments]
Read Entire Article