Arrays of mpz_t

David McKen dmlmcken at
Sat Feb 19 05:03:59 CET 2005

I can confirm that the C++ wrapper mpz_class works both with the new 
operator and with vector<>. Failing that (and assuming you want to use 
C) you can do the following:

unsigned num_elements = 1000;
mpz_t * my_array;
my_array = malloc(sizeof(mpz_t) * num_elements);

Just curious do you know about the mpz_array_init function? It imposes 
some limitations but it may be useful.

Richard Cavell wrote:

>I've been trying to create an array of mpz_t.  (See comp.lang.c++ for my threads).  The best solution I have is something like this:
>#define MAXMPZ 1000
>mpz_t mympz[MAXMPZ];
>if (NumberOfMPZNeeded>MAXMPZ) {printf("Increase MAXMPZ"); abort();}
>for (int i=0; i<NumberOfMPZNeeded; i++)
> mpz_init(mympz[i]);
> mpz_whatever(mympz[i]);
>This is a bit wasteful of up to MAXMPZ * sizeof (mpz_t) bytes on my heap, plus it's amateurish programming.  Is there a better way?  The new operator fails, as does 

More information about the gmp-discuss mailing list