title: mpz_add() causes segmentation Fault at large program
body: <p>My problem is the following. I have to write a program which calculates a really large element of the Fibonacci numbers (lowest it has to calculate is the  <code>pow(2,10)</code>th member, largest is  <code>pow(2,20)</code>th member). For this I'm using GMP's mpz_t and it's functions for calculations.</p>
  
  <p>I use a tail recursive algorithm for this (later on I have to make it run parallel). The issue is that it runs for a while, then suddenly: <code>Segmentation fault (core dumped)</code>.</p>
  
  <p>I show you my code, explain it, so you don't have to waste your time figuring it out and tell you what I got to know.</p>
  
  <pre><code>int main(int argc, char** argv){
    char result[1000000]; char *r; r = result;
    long int n;
    mpz_t num;
    mpz_init(num);
    double start_t, end_t, total_t;
    start_t = omp_get_wtime();
    for(int i = 0; i &lt; 11; i++){
      n = pow(2,i+10);
      fibo(num,n);
      char *d = mpz_get_str(NULL,10,num);
      strcpy(r,d);
      printf("The %ld. element of Fibonacci is: %s\n",n,result);
      fflush(stdout);
      memset(result,  0, sizeof result);
    }
  
    end_t = omp_get_wtime();
    total_t = end_t - start_t;
  
    printf("Time of running: %.6f\n",total_t);
  
    return 0; 
    }
  </code></pre>
  
  <p>The <code>main()</code> function basically creates (and initializes) the variables, sets up the time measurement and in a for loop calls the <code>fibo()</code> function, getting the result and printing it. When everything is done, the program writes out the time of running and quits.</p>
  
  <pre><code>void fibo(mpz_t res, long int n){
    if(n == 0){
        mpz_set_str(res,"0",10);      
        return;
    }else{
    mpz_t temp1;
    mpz_t temp2;
    mpz_init_set_si(temp1,0);
    mpz_init_set_si(temp2,1);
    fiboTail(res,n,1,temp1,temp2);
    mpz_clear(temp1);
    mpz_clear(temp2);
  }
  
  }
  </code></pre>
  
  <p><code>fibo()</code> gets 2 arguments, first one is mpz_t (for the ones who don't know, this is a pointer and it's going to belong to the one that got created in the <code>main()</code> so the final value is going to land back there for further usage) and the second one is the number of the element we need to calculate. If the element number is 0, we simply give back "0", otherwise we make two mpz_t variables, set one two "0", the other to "1" and hand them to the <code>fiboTail()</code> along with some other arguments.</p>
  
  <pre><code>void fiboTail(mpz_t res, long int n, long int m, mpz_t fibPrev, mpz_t fibCurrent){
      if(n == m){
        mpz_set(res,fibCurrent);
      }else{
        mpz_add(fibPrev,fibPrev,fibCurrent);
        fiboTail(res,n, m + 1, fibCurrent, fibPrev);
      }
  }
  </code></pre>
  
  <p>So this one basically is the <strong>core</strong>. <code>m</code> counts how many additions we have done, on which element we are at, <code>n</code> is the number of element we need, <code>fibCurrent</code> and <code>fibPrev</code> is the current and previous Fibonacci number respectively. </p>
  
  <p>Sorry for the dumb explanation, I figure most of you knew this without me trying to explain.</p>
  
  <p>So, this program is really fast. The problem (<code>Segmentation fault</code>) happens when it's counting the 131072th element (sometimes on a smaller one, its...random(?)). Then the program stops about the same number of addition/<code>m</code> value (not always on the same one, but close to there) and the previously mentioned error message appears. I use <code>gcc</code> to compile (actually using Makefiles), so I added the <code>-g</code> switch and used <code>gdb</code> to get more info. Here is what I found:</p>
  
  <p>I ran the program in <code>gdb</code> and used <code>backtrace</code> which produce <a href="http://i.imgur.com/n8iOJb7.png" rel="nofollow">this</a>.</p>
  
  <p><a href="http://i.imgur.com/MStxiLk.png" rel="nofollow">Here</a> is the detailed stack info using <code>info frame</code> on frame #0-5. The error occurs at the mpz_add call, but I don't know why.</p>
  
  <p>If you need any more information, I can give them, but for now I don't know what else would be useful. </p>
  
  <p>Sorry for the long post, thanks for the answers in advance!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>As it seems that <code>mpz_add</code> dies at a point, I got out the info of the call, you can see it: i.imgur.com/XOpTve1.png (Sorry, can't post more than 2 links :/ )</p>