title: undefined behaviour on dynamic allocated arrays (segmentation fault)
body: <p>This program calculates Fibonacci series of the form: t1, t2, t3 = t1 + t^2, t4 = t2+t3^2 ...</p>
  
  <p>It takes three inputs t1, t2 and nth number in the series you want to calculate. </p>
  
  <p>Since the numbers increase exponentially, i used arrays to display and perform manipulations on large numbers. </p>
  
  <h1>PROBLEM:</h1>
  
  <p>I'm stuck with some weird error on:</p>
  
  <pre><code>bsquare[j] = *b[j]
  </code></pre>
  
  <p>use inputs 0 1 8 to recreate the error, it works fine for inputs upto 7. also i tried debugging using gdb, but the debugger shows that memory is accessible when i use p **b@50 and bn also seems to be valid. Is there some undefined behaviour i'm missing out on?</p>
  
  <h2>Code</h2>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  int carry = 0;
  int fibo( int **a, int **b, int **c, int n);
  void sum(int *a, int *b, int *c, int *an, int *bn, int *cn);
  void sqr(int *b, int *bn);
  int main()
  {   
  
      int *a, *b, *c;
      int t1, t2, n, i, bn;
  
      scanf("%d%d%d",&amp;t1, &amp;t2,&amp;n);
  
      a = (int * ) calloc(100, sizeof(int));
      b = (int * ) calloc(100, sizeof(int));
      c = (int * ) calloc(100, sizeof(int));
      a[0] = t1; b[0] = t2;
  
      bn = fibo(&amp;a,&amp;b,&amp;c,n);
  
      printf("\nNumber is: ");
      for(i = bn-1; i&gt;=0; i--)
      printf("%d",b[i]);
  
      free(a);free(b);free(c);
      return 0;
  }
  void sqr(int *b, int *bn)
  {   int c[50] = {0};
      int i , j ,k = 0;
      int temp = 0;
      for(i = 0; i &lt; *bn; i++)
      {    k = i;
          carry = 0;
          for(j = 0; j &lt; *bn ;j++)
          {
              temp = c[k] + b[i] * b[j] + carry;
              if(temp&gt;9)
              {
                  carry = temp / 10;
                  c[k] = temp % 10;
  
              }
              else
              {
                  c[k] =  temp;
                  carry = 0;
              }
              k++;
          }
          if(carry !=0)
          {c[k++] = carry;
          }
  
      }
  
      for(i = 0; i&lt;k; i++)
      {   
          b[i] = c[i];
  
      }
  
      *bn = k;
  
  
  
  }
  
  void sum(int *a, int *b, int *c, int *an, int *bn, int *cn)
  {
  
      int i=0 , j ,k = 0;
      int *temp1;
      int temp = 0;
      for(j=0; j&lt;*cn; j++)
       c[j] = 0;
              *cn = 0;
       carry = 0;
      for(; i &lt; *bn &amp;&amp; i &lt; *an; i++)
      {   
          temp = a[i] + b[i] + carry;
  
          if(temp&gt;9)
          {
              c[k] = temp % 10;
              carry = temp/10;
  
          }
          else
          {
              c[k] = temp;
              carry = 0;
          }
          k++;
      }
  
      while(i&lt;*an)
      {
          temp = carry + a[i];
          if(temp&gt;9)
          {
              c[k] = temp % 10;
              carry = temp/10;
  
          }
          else
          {
              c[k] = temp;
              carry = 0;
          }
          k++;i++;
      }
  
      while(i&lt;*bn)
      {
          temp = carry + b[i];
          if(temp&gt;9)
          {
              c[k] = temp % 10;
              carry = temp/10;
  
          }
          else
          {
              c[k] = temp;
              carry = 0;
          }
          k++;i++;
      }
      if(carry!=0)
      {
          c[k++] = carry;
      }
  
      *cn = k;
  
  
  
  
  }
  
  int fibo( int **a, int **b, int **c, int n)
  {
      int i, j,*tmpdb;
  
      int bn = 1, an = 1, cn = 0, bsn = 0, temp;
      int bsquare[100] = {0};
  
      for(i = 3; i&lt;= n; i++)
          {   
              for(j = 0; j&lt;bn; j++)
                  bsquare[j] = *b[j];
  
              bsn = bn;
  
              sqr(bsquare,&amp;bsn);
              sum(*a,bsquare,*c,&amp;an,&amp;bsn,&amp;cn);
  
              tmpdb = *a;
              *a = *b;
              *b = *c;
              *c = tmpdb;
  
              temp = an;
              an = bn;
              bn = cn;
              cn = temp;
  
  
  
          }
      return bn;
  
  }
  </code></pre>