Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

When is goto not "considered harmful"?

OpenStudy (farmdawgnation):

Well, I don't know about "harmful" - but it's pretty widely accepted that goto is bad form regardless of what language you're using.

OpenStudy (anonymous):

How about finite state machines?

OpenStudy (farmdawgnation):

I mean, they're pretty cool. haha. How exactly do they relate to "goto"?

OpenStudy (anonymous):

represent state transitions as gotos

OpenStudy (anonymous):

goto's are harmful when the location it is pointing to is null or undefined. I mean that if the code goes this way goto xy; and xy is not present or it has some malicious code then there is either an exception or the whole program is corrupted.

OpenStudy (anonymous):

Right, but that didn't answer my question... :(

OpenStudy (anonymous):

goto is pretty well accepted as a style for unwinding initialization code

OpenStudy (anonymous):

that's a case when they're not harmful

OpenStudy (anonymous):

for example bool func() { char *a, *b, *c, *d; if ((a = malloc(1000)) == NULL) goto fail; if ((b = malloc(1000)) == NULL) goto b_failed; if ((c = malloc(1000)) == NULL) goto c_failed; if ((d = malloc(1000)) == NULL) goto d_failed; return TRUE; // success! d_failed: free(c); c_failed: free(b); b_failed: free(a); fail: return FALSE; // failure } This is a pretty common style that unwinds initialization if a multi-step initializaition procedure fails. It's common practice in the linux kernel.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!