Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cannot 'new' a GMP object

Reply
Thread Tools

Cannot 'new' a GMP object

 
 
Richard Cavell
Guest
Posts: n/a
 
      02-18-2005
#include <gmp.h>

mpz_t* p_mympz = new mpz_t;

error: cannot convert '__mpz_struct*' to '__mpz_struct (*)[1]' in
initialization

1. What does that mean?

2. Given that mpz_t doesn't have a constant sizeof() I take it that
dynamic creation (new/delete) is the only way I can create an array of
them (that is, keep their pointers in an array and use the -> operator).
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-18-2005
Richard Cavell wrote:

> #include <gmp.h>
>
> mpz_t* p_mympz = new mpz_t;
>
> error: cannot convert '__mpz_struct*' to '__mpz_struct (*)[1]' in
> initialization
>
> 1. What does that mean?


Seems that mpz_t is a typedef for an array. If you allocate an array with
new, you get a pointer to the first element, not a pointer to the array.
Anyway, I wonder why there would be a typedef for an array with exactly one
element.

> 2. Given that mpz_t doesn't have a constant sizeof()


What do you mean? sizeof() is resolved at compile time. It _always_ returns
a compile-time constant.

> I take it that dynamic creation (new/delete) is the only way I can create
> an array of them (that is, keep their pointers in an array and use the ->
> operator).


Well, if the number of elements is not know until it is created, yes.
However, you can use std::vector instead, which handles the memory
management details for you.

 
Reply With Quote
 
 
 
 
Richard Cavell
Guest
Posts: n/a
 
      02-18-2005
On 18/2/05 8:47 PM, Rolf Magnus wrote:

> Seems that mpz_t is a typedef for an array. If you allocate an array with
> new, you get a pointer to the first element, not a pointer to the array.
> Anyway, I wonder why there would be a typedef for an array with exactly one
> element.


typedef __mpz_struct mpz_t[1];

__mpz_struct is typedef'ed to a struct which has 2 ints and one pointer.
The GMP code is too advanced for me but I'm guessing that mpz_t[1] is
intended to point to the end of the structure rather than the start.

> What do you mean? sizeof() is resolved at compile time. It _always_ returns
> a compile-time constant.


I'm making the point, though, that if I could know that sizeof() were
constant I could create vector<mpz_t> instead of vector<mpz_t*>
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      02-18-2005
On Fri, 18 Feb 2005 21:56:33 +1100, Richard Cavell
<> wrote:

> On 18/2/05 8:47 PM, Rolf Magnus wrote:
>
>> Seems that mpz_t is a typedef for an array. If you allocate an array with
>> new, you get a pointer to the first element, not a pointer to the array.
>> Anyway, I wonder why there would be a typedef for an array with exactly one
>> element.

>
> typedef __mpz_struct mpz_t[1];
>
> __mpz_struct is typedef'ed to a struct which has 2 ints and one pointer.
> The GMP code is too advanced for me but I'm guessing that mpz_t[1] is
> intended to point to the end of the structure rather than the start.


It's declared that way so that a C program can pass it as a sort of
reference parameter:

mpz_t a, b, c;
mpz_init2(a, 1);
mpz_init2(b, 2);
mpz_init(c);
mpz_add(c, a, b);

instead of having to put & in front of all of the variables to pass them
as pointers explicitly. Unfortunately this really messes with the types
in C++, and I wouldn't guarantee that vector<> and other containers work
properly (I remember having some problems at one time, but I'm not sure
whether they now work).

>> What do you mean? sizeof() is resolved at compile time. It _always_ returns
>> a compile-time constant.

>
> I'm making the point, though, that if I could know that sizeof() were
> constant I could create vector<mpz_t> instead of vector<mpz_t*>


Try using the C++ wrapper instead (mpz_class). Its major disadvantage
is that it sets up each variable every time it is created, including
temporaries:

mpz_class a, b, c;
a = 1;
b = "2000";
c = a + (a * b);

will allocate a temporary for (a * b) and initialise it, then throw it
away after use, calling malloc() and free(), which takes a lot of time.
If you stick to simple expressions though this won't happen (it has
optimisations so that (x = a + b; and the like are done as a single
operation). For instance, I've done timings on the following:

r = (a*b + c*d) / (a + b*c + d);

r = a*b;
tmp = c*d;
r += tmp;
tmp = b*c;
tmp += a;
tmp += d;
r /= tmp;

and the differences (on Debian GNU/Linux 'woody', Duron 1200 CPU) are a
factor of 3-4 for mpz_class and a factor of 2 for mpf_class (floating
point, both with default precision 12. The second form is comparable
in speed to using the C GMP functions directly.

Chris C
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.net 2.0 - GMP Services Inc Announces GMP Content Management System 2.0 Allen Harkleroad ASP .Net 0 09-05-2007 06:17 PM
GMP: Cannot operate on vector<mpz_t> Richard Cavell C++ 3 02-18-2005 11:32 PM
GMP :: passing GMP integer values using functions:: HELP sam1967@hetnet.nl C Programming 4 11-23-2004 11:31 AM
help needed, Can't locate loadable object for module GMP--AIX Senthilkumar T Perl Misc 4 07-15-2004 01:44 PM
"Can't locate loadable object for module GMP" - any ideas Jasper Perl Misc 4 06-29-2004 09:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57