title: GMP mpz_t variable being set with incorrect value
body: <p>I've been working on the Euler 29 problem for a few days and am having difficulty getting the mpz_t type to work correctly. The objective is to iterate through a^b for values of 2 &lt;= a,b &lt;= 100 and count the nonrepeat values.</p>
  <p>Using vector I was able to store the values using pointers in an array like so:</p>
  <pre><code>mpz_t a;
  mpz_init(a);
  vector&lt;mpz_t*&gt; numbers;
  numbers.push_back(&amp;a);
  </code></pre>
  <p>However, when running the full program below you can see that after it inserts the first value 4, it doesn't insert any new values. This is because the temp value being compared to rop is not being set to what is already in the array, and instead is set to the value shared by rop.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;chrono&gt;
  #include &quot;gmp.h&quot;
  
  using namespace std;
  
  int main()
  {
      auto start = std::chrono::high_resolution_clock::now();
      
      int solution = 0;
      bool found = false;
      int r = 10;
      
      mpz_t rop;
      mpz_init(rop);
      
      mpz_t temp;
      mpz_init(temp);
      
      vector&lt;mpz_t*&gt; numbers;
      
      for(int a = 2; a &lt;= 5; a++)
      {
          for(int b = 2; b &lt;= 5; b++)
          {
              mpz_ui_pow_ui(rop, a, b);
              for(int i = 0; i &lt; numbers.size(); i++)
              {
                  cout &lt;&lt; &quot;i: &quot; &lt;&lt; i &lt;&lt; endl;
                  cout &lt;&lt; &quot;rop: &quot;;
                  mpz_out_str(stdout,10,rop);
                  cout &lt;&lt; endl;
                  
                  mpz_set(temp,*(numbers.at(i)));
                  cout &lt;&lt; &quot; temp: &quot;;
                  mpz_out_str(stdout,10,temp);
                  cout &lt;&lt; endl;
                  
                  r = mpz_cmp(rop,temp);
                  cout &lt;&lt; &quot; r: &quot; &lt;&lt; r &lt;&lt; endl &lt;&lt; endl;
                  if(r == 0)
                  {
                      found = true;
                      break;
                  }
              }
              if(found == false)
              {
                  numbers.push_back(&amp;rop);
                  solution++;
                  cout &lt;&lt; &quot;pushed! &quot; &lt;&lt; endl &lt;&lt; endl;
              }
              found = false;
          }
      }
  
      auto done = std::chrono::high_resolution_clock::now();
      cout &lt;&lt; &quot;Solution: &quot; &lt;&lt; solution &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; &quot;Program completed in &quot; &lt;&lt; std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(done - start).count() &lt;&lt; &quot; milliseconds.&quot; &lt;&lt; endl;
  }
  </code></pre>
  <p>This line of code should be setting temp equal to 4 at the start of the forloop, but instead sets it equal to rop:</p>
  <pre><code>mpz_set(temp,*(numbers.at(i)));
  </code></pre>
  <p>Since the problem clearly has to do with the fact I'm using pointers and passing the actual address in memory to store these mpz_t variables, how can I change the code so that it is able to work properly? I'm under the impression using the function mpz_clear(rop) after each push_back to the numbers vector wouldn't work as it releases the address from memory.</p>