{"id":1245,"date":"2022-08-30T15:14:48","date_gmt":"2022-08-30T15:14:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/why-is-bounds-checking-not-implemented-in-some-of-the-languages-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:48","modified_gmt":"2022-08-30T15:14:48","slug":"why-is-bounds-checking-not-implemented-in-some-of-the-languages-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-is-bounds-checking-not-implemented-in-some-of-the-languages-collection-of-common-programming-errors\/","title":{"rendered":"Why is bounds checking not implemented in some of the languages?-Collection of common programming errors"},"content":{"rendered":"<p>Basically, it&#8217;s because it means every time you change an index, you have to do an if statement.<\/p>\n<p>Let&#8217;s consider a simple C for loop:<\/p>\n<pre><code>int ary[X] = {...};  \/\/ Purposefully leaving size and initializer unknown\n\nfor(int ix=0; ix&lt; 23; ix++){\n    printf(\"ary[%d]=%d\\n\", ix, ary[ix]);\n}\n<\/code><\/pre>\n<p>if we have bounds checking, the generated code for <code>ary[ix]<\/code> has to be something like<\/p>\n<pre><code>LOOP:\n    INC IX          ; add `1 to ix\n    CMP IX, 23      ; while test\n    CMP IX, X       ; compare IX and X\n    JGE ERROR       ; if IX &gt;= X jump to ERROR\n    LD  R1, IX      ; put the value of IX into register 1\n    LD  R2, ARY+IX  ; put the array value in R2\n    LA  R3, Str42   ; STR42 is the format string\n    JSR PRINTF      ; now we call the printf routine\n    J   LOOP        ; go back to the top of the loop\n\n;;; somewhere else in the code\nERROR:\n    HCF             ; halt and catch fire\n<\/code><\/pre>\n<p>If we don&#8217;t have that bounds check, then we can write instead:<\/p>\n<pre><code>    LD R1, IX\nLOOP:\n    CMP IX, 23\n    JGE END\n    LD R2, ARY+R1\n    JSR PRINTF\n    INC R1\n    J   LOOP\n<\/code><\/pre>\n<p>This saves 3-4 instructions in the loop, which (especially in the old days) meant a lot.<\/p>\n<p>In fact, in the PDP-11 machines, it was even better, because there was something called &#8220;auto-increment addressing&#8221;. On a PDP, all of the register stuff etc turned into something like<\/p>\n<pre><code>CZ  -(IX), END    ; compare IX to zero, then decrement; jump to END if zero\n<\/code><\/pre>\n<p>(And anyone who happens to remember the PDP better than I do, don&#8217;t give me trouble about the precise syntax etc; you&#8217;re an old fart like me, you know how these things slip away.)<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Basically, it&#8217;s because it means every time you change an index, you have to do an if statement. Let&#8217;s consider a simple C for loop: int ary[X] = {&#8230;}; \/\/ Purposefully leaving size and initializer unknown for(int ix=0; ix&lt; 23; ix++){ printf(&#8220;ary[%d]=%d\\n&#8221;, ix, ary[ix]); } if we have bounds checking, the generated code for ary[ix] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1245","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1245"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}