title: GMP, C language memory allocation and pointer
body: <p>I was trying to code something like this</p>
  
  <pre><code>....
  mpz_class *x = NULL;
  mpz_class *lValue = NULL;
  ....
  for(int k = 0; k &lt; 2; k++) {
      x = NULL;
      lValue = NULL;
      x = (mpz_class*) malloc(sizeof(mpz_class) * exponentForFactors[k]);
      lValue = (mpz_class*) malloc(sizeof(mpz_class) * exponentForFactors[k]);
  
      rValue = 0;
  
      mpz_class exp = (p-1)/q[k];
  
      mpz_powm(lValue[0].get_mpz_t(), B.get_mpz_t(),exp.get_mpz_t(), p.get_mpz_t()); &lt;- this part
  </code></pre>
  
  <p><code>exponentForFactors[k] = {3, 1}</code> 
  this code will loop twice as k is less than 2. 
  At first loop, it is ok. there is no error but when it is the second loop, it has this error message where I pointed. <code>malloc: *** error for object 0x6000000000000000: pointer being realloc'd was not allocated
  *** set a breakpoint in malloc_error_break to debug</code></p>
  
  <p>I do not understand why this thing takes place only at the second loop? 
  Any suggestion would be grateful. Thanks. </p>
  
  <p>////////////////////////////</p>
  
  <p>even this code(when the second loop)</p>
  
  <pre><code>cout &lt;&lt; "lvalue = " &lt;&lt; lValue[0] &lt;&lt; endl;
  </code></pre>
  
  <p>has the problem. </p>