title: segfault due to finite recursion
body: <p>I'm receiving a segfault which I believe to be caused by a large number of recursive calls. This has been noted <a href="https://stackoverflow.com/questions/8202445/segmentation-fault-on-a-recursive-function">here</a>. My confusion is that the recursion is not infinite; there is a definite breaking point once a given event occurs and that event will always occur, eventually. I suspect the issue is that the number of recursive calls before breaking is too many. Output from valgrind:</p>
  <pre><code>==903368== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
  ==903368==
  ==903368== Process terminating with default action of signal 11 (SIGSEGV): dumping core
  ==903368==  Access not within mapped region at address 0x1FFE801FF8
  ==903368== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
  ==903368==    at 0x4081B4: operator-(Point const&amp;, Point const&amp;) (Point.h:47)
  ==903368==  If you believe this happened as a result of a stack
  ==903368==  overflow in your program's main thread (unlikely but
  ==903368==  possible), you can try to increase the size of the
  ==903368==  main thread stack using the --main-stacksize= flag.
  ==903368==  The main thread stack size used in this run was 8388608.
  ==903368== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
  ==903368==
  ==903368== Process terminating with default action of signal 11 (SIGSEGV)
  ==903368==  Access not within mapped region at address 0x1FFE801FE8
  ==903368== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
  ==903368==    at 0x482F14D: _vgnU_freeres (vg_preloaded.c:57)
  ==903368==  If you believe this happened as a result of a stack
  ==903368==  overflow in your program's main thread (unlikely but
  ==903368==  possible), you can try to increase the size of the
  ==903368==  main thread stack using the --main-stacksize= flag.
  ==903368==  The main thread stack size used in this run was 8388608.
  ==903368==
  ==903368== HEAP SUMMARY:
  ==903368==     in use at exit: 86,717 bytes in 40 blocks
  ==903368==   total heap usage: 52,536 allocs, 52,496 frees, 47,716,014 bytes allocated
  ==903368==
  ==903368== LEAK SUMMARY:
  ==903368==    definitely lost: 0 bytes in 0 blocks
  ==903368==    indirectly lost: 0 bytes in 0 blocks
  ==903368==      possibly lost: 0 bytes in 0 blocks
  ==903368==    still reachable: 86,717 bytes in 40 blocks
  ==903368==         suppressed: 0 bytes in 0 blocks
  ==903368== Rerun with --leak-check=full to see details of leaked memory
  ==903368==
  ==903368== For lists of detected and suppressed errors, rerun with: -s
  ==903368== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  Segmentation fault (core dumped)
  </code></pre>
  <p>Attempt to reproduce:</p>
  <pre><code>void f(int i, const int&amp; niter) {
      i++;
      if (i &lt; niter)
          f(i, niter);
      else 
          return;
  }
  
  int main()
  {
      // fine
      f(0, 1000);
      
      // not fine
      f(0, 1000000);
      return 0;
  }
  </code></pre>
  <p>Should I be increasing the main stack size as suggested by valgrind? Or just avoid the recursion altogether?</p>
  <pre><code>int main()
  {
      int i = 0;
      while (i &lt; 1000000) 
          i++;
  }
  </code></pre>