GMP: Cannot operate on vector<mpz_t>

R

Richard Cavell

#include <gmp.h>

vector<mpz_t> v_bigints; v_bigints.resize(20);

This fails.

error: ISO C++ forbids casting to an array type '__mpz_struct[1]' (GCC
on a G4)

What's wrong with this? (GMP mailing lists were closed today, sorry).
 
K

Karl Heinz Buchegger

Richard said:
#include <gmp.h>

vector<mpz_t> v_bigints; v_bigints.resize(20);

This fails.

error: ISO C++ forbids casting to an array type '__mpz_struct[1]' (GCC
on a G4)

What's wrong with this? (GMP mailing lists were closed today, sorry).

As has been told otherthread, the type mpz_t is defined in a way which
makes this impossible. It is a 'clever hack' to avoid a few keystrokes.
(So much for clever hacks :)

You could try using __mpz_struct directly.

vector < __mpz_struct > v_bigints;
 
C

Chris Croughton

#include <gmp.h>

vector<mpz_t> v_bigints; v_bigints.resize(20);

This fails.

error: ISO C++ forbids casting to an array type '__mpz_struct[1]' (GCC
on a G4)

What's wrong with this? (GMP mailing lists were closed today, sorry).

See my reply in thread "Cannot 'new' a GMP object", Message-ID

<[email protected]>

mpz_t is defined as a 1-element array, to make using the C GMP functions
easier, which blows the C syntax for containers and new. Plus it is
risky, since you will have to ensure that each of the elements is
correctly initialised and cleared whenever you resize the vector
otherwise you will get momory leaks. The C++ class versions are much
easier to us.

Chris C
 
R

Richard Cavell

vector < __mpz_struct > v_bigints;

That doesn't work either.

Thanks to everyone for their help.

For the record, my solution is like this:

mpz_t myMPZArray[3000]; // This works

for (int i=0; i<NumberOfMPZNeeded; i++)
{
// Initialize and operate on myMPZArray;
}

Which wastes up to 3000 * sizeof (mpz_t) bytes of RAM and means I have
to change a constant in my program to get more than 3000, but it does
work...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top