Closed ReperakDev closed 2 years ago
Threat neutralized. Moving out
why did you use goto in the first place?
also, this is pretty good and I think we should merge @MrGlockenspiel
yeah i just didnt have time to look through it before yet
why did you use goto in the first place?
A little late but it's because they're better in every way for error recovery. An oversimplified example is this:
int do_various_shit()
{
char *foo = malloc(sizeof(char) * 50);
int bar = get_bar();
if (!bar) {
return 1;
}
// just pretend that we used bar ;)
free(foo);
return 0;
}
See the problem here? If get_bar fails, foo is never freed.
We could fix it like this:
// snip
int bar = get_bar();
if (!bar) {
free(foo);
return 1;
}
// snip
free(foo);
return 0;
// snip
But that's stupid and ineffcient.
Let's refactor it to use gotos.
int do_various_shit()
{
int ret_code = 0;
char *foo = malloc(sizeof(char) * 50);
int bar = get_bar();
if (!bar) {
ret_code = 1;
goto err;
}
err:
free(foo);
return ret_code;
}
Much better.
I'm @MrGlockenspiel's friend so I get merge priority :trollface: