Connecting Tech Pros Worldwide Forums | Help | Site Map

GMP: Cannot operate on vector<mpz_t>

Richard Cavell
Guest
 
Posts: n/a
#1: Jul 23 '05
#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).

Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 23 '05

re: GMP: Cannot operate on vector<mpz_t>


Richard Cavell wrote:[color=blue]
>
> #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).[/color]

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;


--
Karl Heinz Buchegger
kbuchegg@gascad.at
Chris Croughton
Guest
 
Posts: n/a
#3: Jul 23 '05

re: GMP: Cannot operate on vector<mpz_t>


On Fri, 18 Feb 2005 22:28:52 +1100, Richard Cavell
<richardcavell@mail.com> wrote:
[color=blue]
> #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).[/color]

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

<slrnd1bo2n.c9h.chris@ccserver.keris.net>

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
Richard Cavell
Guest
 
Posts: n/a
#4: Jul 23 '05

re: GMP: Cannot operate on vector<mpz_t>


On 19/2/05 12:18 AM, Karl Heinz Buchegger wrote:[color=blue]
> vector < __mpz_struct > v_bigints;[/color]

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[i];
}

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...
Closed Thread