4 ms·
> The classic solution to do resource management in C is to (ab)use goto Why "(ab)use" goto ? Use it with pleasure! Solution from Linus Torwalds: int some
by ruslan 3y ago
> The classic solution to do resource management in C is to (ab)use goto
Why "(ab)use" goto ? Use it with pleasure! Solution from Linus Torwalds:
int somefunc(...) {
int ret = E_OK;
FILE *a = NULL;
FILE *b = NULL;
FILE *c = NULL;
if(!(a == fopen("a.txt",...)) {
ret = E_A_FAILED;
goto end;
}
if(!(b == fopen("b.txt",...)) {
ret = E_B_FAILED;
goto end;
}
if(!(b == fopen("b.txt",...)) {
ret = E_C_FAILED;
goto end;
}
/* do your fancy stuff here */
end:
if(a)
fclose(a);
if(b)
fclose(b);
if(c)
fclose(c);
return ret;
}
1. This code is clean and neat.
2. People should read Linux kernel source code as their go to sleep Bible verse.
- sirwhinesalot 3y agoMentioned in the footnotes ;)
- ruslan 3y agoAh, ok, sorry. When I see someone to curse goto I become mad! goto statement is God sent gift.
- sirwhinesalot 3y agoI couldn't implement this defer macro without it.
- ruslan 3y agoBtw, using goto to different labels is no good for modern CPUs as it spoils branch predictor. In classic Linux way it's usually goto to same "end" or "fail" label resulting in more efficient code. Let's hope compiler will rule this out. ;)
- sirwhinesalot 3y agoWhat you save in the gotos you "lose" in the ifs.
- sgbeal 3y ago> This code is clean and neat. And severely flawed. The (a == fopen...), etc, are certainly not doing what is apparently intended.
- ruslan 3y agoSure. There should be only one =. Sorry.
- dazilcher 3y agoIf you're going to embrace goto, why not go all the way and use multiple exit labels such that you can jump to appropriate point in the cleanup sequence and avoid those conditionals?