Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@kvsm if you takve care of your memory yourself in C i.e. Microsoft coding standards require goto cleanup whenever you allocate memory
-
@kvsm I second @slinavipuz. Think of a function that mallocs memory several times. If the second malloc fails you should free the first but not the others.
If this third fails you should free the first two but not the third.
That's how it's done in the Linux kernel.
a = alloc
if a failed goto a_fail
b = alloc
if b failed goto b_fail
c = alloc
if c failed goto c_fail
stuff
free c
c_fail:
free b
b_fail:
free a
a_fail:
return -
@Gauthier it is kinda natural - unconditional jump in ASM. There is a valid use case. But we've all been told scary stories 'bout it.
-
@slinavipuz Which is a good thing. You have to scare students with gotos, because it's always the last solution. Coding the above if if/else would be an abomination if you have more than 2 allocs.
Maybe there should be a license to goto. Only allowed with 5 years pro experience, or something... -
@Gauthier well - I agree. Don't use it unless you're fucking absolute, 100%, sure as hell, p=1, without any doubt, sure that you know what you are fucking doing.
But goto is not evil. People are. Stupid and evil.
Related Rants
I do not think that GoTo is bad. It can lead to hellish code but if you don't misuse it - it can be extremely useful.
undefined
goto
best practices