title: Recursion reverse indexing std::array
body: <p>This is a simple piece of code that uses recursion:</p>
  
  <pre><code>#include &lt;array&gt;
  #include &lt;cstddef&gt;
  #include &lt;iostream&gt;
  
  using std::array;
  using std::size_t;
  using std::cout;
  using std::endl;
  
  #define ARRAY_SZ 3
  
  enum class direction{forward, backward};
  
  int circular (array&lt;size_t,ARRAY_SZ&gt;::iterator, array&lt;size_t, ARRAY_SZ&gt;::iterator, direction);
  
  int main (void){
  
      array&lt;size_t,ARRAY_SZ&gt; A = {5,1,3};
      array&lt;size_t,ARRAY_SZ&gt; B = {4,2,2};
  
      auto result = 0;
      //direction d = direction::forward;
      size_t distance = ARRAY_SZ - 1;
      direction d = direction::backward;
  
      if(d == direction::forward){
         result = circular(A.begin(),B.begin(), d);
         cout &lt;&lt; result &lt;&lt; endl;
      }
      else{
              result = circular(A.begin() + distance, B.begin() + distance, d);
          if(result &gt;= 0){
              cout &lt;&lt; ARRAY_SZ - (result + 1) &lt;&lt; endl;
          }
          else{
              cout &lt;&lt; result &lt;&lt; endl;
          }
      }
  
      return 0;
  }
  
  int circular (array&lt;size_t,ARRAY_SZ&gt;::iterator iterA, array&lt;size_t,ARRAY_SZ&gt;::iterator iterB, direction d){
  
      static auto count = -1; 
      static size_t diff = 0;
  
      switch(d){
          case direction::backward:
              {
                  if(*iterA &gt; *iterB){
                      diff += *iterA - *iterB;
                      ++count;
                      return circular(--iterA, --iterB, d);
                  }
                  else{
                          if(*iterB - *iterA &lt;= diff){
                              diff -= *iterB - *iterA;
                              ++count;
                              return circular(--iterA, --iterB, d);
                          }
                          else{
                              return count;
                          }
                  }
              }
              break;
          default:
              {
                  if(*iterA &gt; *iterB){
                      diff += *iterA - *iterB;
                      ++count;
                      return circular(++iterA, ++iterB, d);
                  }
                  else{
                          if(*iterB - *iterA &lt;= diff){
                              diff -= *iterB - *iterA;
                              ++count;
                              return circular(++iterA, ++iterB, d);
                          }
                          else{
                              return count;
                          }
                  }
              }
              break;
      }
  }
  </code></pre>
  
  <p>The recursive function is </p>
  
  <pre><code>int circular (array&lt;size_t,ARRAY_SZ&gt;::iterator, array&lt;size_t, ARRAY_SZ&gt;::iterator, direction);
  </code></pre>
  
  <p>The implemented logic inside this function works as expected for the forward indexing of both the arrays; the recursion ends as expected returning a value of <code>2</code> for <code>count</code>. </p>
  
  <p>However, in the case of reverse indexing, the recursion goes one iteration beyond the expected end point, as evinced by the following <code>gdb</code> log:</p>
  
  <pre><code>Thread 1 "a" hit Breakpoint 1, circular (iterA=0xffffcb78, iterB=0xffffcb58, d=direction::backward) at code.cpp:49
  49          switch(d){
  (gdb) n
  52                      if(*iterA &gt; *iterB){
  (gdb) n
  53                          diff += *iterA - *iterB;
  (gdb) p *iterA
  $12 = 6445809520
  (gdb) p *iterB
  $13 = 6445644288
  (gdb) 
  </code></pre>
  
  <p>As shown in the log, both <code>iterA</code> and <code>iterB</code> now point to garbage values. </p>
  
  <p>Why is this only observed during reverse indexing and not during forward indexing? </p>