{"id":4816,"date":"2021-08-29T16:57:04","date_gmt":"2021-08-29T21:57:04","guid":{"rendered":"https:\/\/www.paleotechnologist.net\/?p=4816"},"modified":"2021-08-29T16:57:06","modified_gmt":"2021-08-29T21:57:06","slug":"for-loop-gotchas","status":"publish","type":"post","link":"https:\/\/www.paleotechnologist.net\/?p=4816","title":{"rendered":"For Loop Gotchas"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Be careful when writing loops with unsigned variables. The compiler&#8217;s idea of an end condition might not match yours.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When working on a program to produce fractal laser-engraver art, I stumbled across a strange problem. I could call a function to draw a fractal at levels 0 through 6, all of which worked fine, whether individually or called in a for loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When I went to draw them in reverse order, however, the program crashed &#8212; and it didn&#8217;t make sense as to why. The code in question was something like:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>    dim as ulongint x<br>    for x=6 to 0 step -1<br>        drawObject(x)<br>        next x<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This worked fine forwards (for x=0 to 6) but crashed somewhere around level 2 when run in reverse &#8212; even though all levels worked great individually.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I eventually figured out that the bug was due to the variable in the for loop. Instead of iterating from 6 down to zero and then stopping, for loops (in FreeBasic as well as C) blindly decrement the variable and then check the end condition. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, in C, a line which reads<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>    for(uint8_t x=6;x>=0;x--){doTheThing(x);} <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8230;<em>will never return<\/em>. It will get down to zero, decrement and wrap back to all-ones. Since this is a value greater than zero, it will continue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the case of the fractal image, I had unknowingly asked it to compute an 18.4667-quintillion-level Koch curve. This figure would have four-to-the-18.4667-quintillionth-power segments. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No wonder it crashed!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Be careful when using for loops with unsigned variables&#8230; <a href=\"https:\/\/www.paleotechnologist.net\/?p=4816\">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":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[236,249,216,633],"tags":[],"class_list":["post-4816","post","type-post","status-publish","format-standard","hentry","category-basic","category-c","category-coding","category-fractals"],"_links":{"self":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/4816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4816"}],"version-history":[{"count":5,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/4816\/revisions"}],"predecessor-version":[{"id":4821,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/4816\/revisions\/4821"}],"wp:attachment":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}