title: Segmentation Fault (core dumped) while using GMP library
body: <p>I have a problem with the following function. I am trying to implement the &quot;exponentiation by squaring&quot;, which is a general method for fast computation of large positive
  integer powers of a number.</p>
  <pre><code>unsigned long EBS(mpz_t &amp;m, mpz_t g, mpz_t k, mpz_t p) {
          mpz_t y, carre, a;
          mpz_init(y);
          mpz_set_ui(carre, 2);
  
          if(mpz_cmp_si(k, 0) &lt; 0) {
              mpz_set_ui(a, 1);
              mpz_cdiv_q(g, a, g);
              mpz_mul_si(k, k, -1);
          }
          if(mpz_cmp(k, 0) == 0) {
              mpz_set_ui(m, 1);
              return mpz_get_ui(m);
          }
      
          mpz_set_ui(y, 1);
          
          while (mpz_cmp_si(k, 1) &gt; 0) {
              if(mpz_even_p(k) != 0) {
                  mpz_powm(g, g, carre, p);
                  mpz_set_si(k, (mpz_get_si(k) / 2));
              }
              else {
                  mpz_mul(y, g, y);
                  mpz_mul(g, g, g);
                  mpz_set_si(k, ((mpz_get_si(k) - 1) / 2));
              }
          }
          mpz_mul(m, g, y);
          
          return mpz_get_ui(m);
   }
  </code></pre>
  <p>When I call the function from <code>main()</code> :</p>
  <pre><code>int main() {
     mpz_t M,c;
     mpz_init(M);
     mpz_init(c);
  
     .....
  
   const char* mess = random_String();
   char c_str[1000];
   std::cout &lt;&lt; &quot;Le message à chiffrer : &quot; &lt;&lt; mess &lt;&lt; std::endl;
   mpz_set_str(M, mess, 10);
   EBS(c, M, e, n); //I call the function from here
  
  }
  </code></pre>
  <p>When I call the function <code>EBS</code>, I get an <code>Erreur de segmentation (core dumped)</code> and I don't understand why.
  Can you please help me with this?</p>