{"id":2622,"date":"2017-07-29T00:01:34","date_gmt":"2017-07-29T07:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2622"},"modified":"2017-07-22T10:26:33","modified_gmt":"2017-07-22T17:26:33","slug":"when-goto-might-be-necessary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2622","title":{"rendered":"When <em>goto<\/em> Might Be Necessary"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2615\">last week&#8217;s Lesson<\/a>, I covered the <em>goto<\/em> keyword the way most instructors do: I explained that it&#8217;s not needed and admonished you not to use it lest you be labeled an complete and utter dork. Still, why would such a superfluous method of program control be included if it&#8217;s awkward and unnecessary?<br \/>\n<!--more--><br \/>\nIn their seminal tome, <em>The C Programming Language<\/em>, Kernighan and Ritchie write about the <em>goto<\/em> statement. I included the first part of the quote in last week&#8217;s Lesson. The rest of it is:<\/p>\n<blockquote><p>&#8230;we will suggest a few situations where <em>goto<\/em>&#8216;s [sic] may find a place. the most common use is to abandon processing in some deeply nested structure, such as breaking out of two loops at once.<\/p><\/blockquote>\n<p>K&#038;R then proceed to demonstrate a code snippet where <em>goto<\/em> breaks out of a nested <em>for<\/em> loop, but then they immediately show a more readable example that does the same thing without using <em>goto<\/em>. Again, they prove that <em>goto<\/em> is never necessary.<\/p>\n<p>Yet, the example was too innocent. In fact, <em>goto<\/em> would theoretically be necessary in a large program to provide an exit from one or more routines that otherwise have no easy way out.<\/p>\n<p>Consider code where you have multiple modules and functions, several of which can error but at the point of the error you may not know exactly how to unwind loops and return from functions to access the error-management routines. I&#8217;ve encountered this situation in my coding several times, primarily for large, multi-module and multitasking programs. Even then, I&#8217;ve never used <em>goto<\/em>.<\/p>\n<p>Instead of <em>goto<\/em>, the solution I employ are two functions designed specifically to deal with program recovery: <em>setjmp()<\/em> and <em>longjmp()<\/em>. I find it interesting that both functions use the assembly language <em>jmp<\/em> for &#8220;jump,&#8221; which is what <em>goto<\/em> effectively is.<\/p>\n<p>The <em>setjmp()<\/em> function saves the program&#8217;s environment, with such details as the stack, variable values, and other program conditions. It serves as an anchor, a re-entry point into your code from another location.<\/p>\n<p>The <em>longjmp()<\/em> function bails out of a location deeper in your code. It&#8217;s called after the <em>setjmp()<\/em> function and it returns execution to the spot in the code where <em>setjmp()<\/em> was called. Further, it returns a value to <em>setjmp()<\/em> so your code can evaluate what happened, and so that <em>setjmp()<\/em> knows whether it&#8217;s being initialized or handling a return.<\/p>\n<p>Both functions require the <code>setjmp.h<\/code> header file. They both use a variable of the <em>jmp_buf<\/em> type to retain the program environment.<\/p>\n<p>Yes, these functions work like a <em>goto<\/em>, but they&#8217;re far more logical and useful to help you out of tight situations.<\/p>\n<p>I can&#8217;t show my code here as an example because it&#8217;s thousands of lines long. Basically, I wrote a communications program that spawned multiple processes to monitor incoming data. Upon a disconnect, the <em>longjmp()<\/em> function bailed out of whichever routine detected the dropped signal and the <em>setjmp()<\/em> function reset the program. That way I didn&#8217;t have to figure out which routine called the processes, break out of unknown loops, or reset variables: <em>setjmp()<\/em> and <em>longjmp()<\/em> did the work for me.<\/p>\n<p>Bottom line: K&#038;R are correct. The <em>goto<\/em> statement is never necessary. No matter which way you try to paint your code into a corner, you can always flee without needing a <em>goto<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Even with the exception noted by Kernighan and Ritchie, you still don&#8217;t ever need a <em>goto<\/em> in your code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2622\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2622","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2622"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2622\/revisions\/2630"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}