placing mpz_t variable of gmp.h library into the char buffer

V

vaidehikedlaya

Hello,
I am using gmp.h library.
I am trying to put mpz_t variable into char buffer and back. I came
across mpz_import/mpz_export to suffice this purpose. But, I am not
very clear about using these functions. I am not getting the right
result for the program below. In the program I have included the
code(present in the gmp document, in the description of mpz_export
function)

numb = 8*size - nail;
count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
p = malloc (count * size);

to determine the size of the char buffer. In the above code is "size"
the size of char buffer.
Could you please let me know what is wrong in the code below.
Is there any other way to place mpz_t variable into char buffer, and
the buffer data back to mpz_t variable.

# include<stdio.h>
# include<gmp.h>
int main(){

int base=10;
mpz_t op;
mpz_t z;
mpz_init_set_str(op,"21896",base);

/*Determinimg the size of the char buffer*/
long size = sizeof(char);
long numb = 8*size - 0;
long count = (mpz_sizeinbase (op, 2) + numb-1) / numb;
char *p = (char *)malloc (count * size);

/* The mpz variable is put into buffer p, and back into mpz variable*/
mpz_export(p,NULL,1,1,0,0,op);
mpz_import (z,count*size , 1, sizeof(p), 0, 0, p);

gmp_printf("the value of z : %Zd\n",z);
}


Thanks in advance.
 
S

santosh

Hello,
I am using gmp.h library.

No such library. It's a header file, presumably for the GMP library.
I am trying to put mpz_t variable into char buffer and back. I came
across mpz_import/mpz_export to suffice this purpose. But, I am not
very clear about using these functions. I am not getting the right
result for the program below. In the program I have included the
code(present in the gmp document, in the description of mpz_export
function)

numb = 8*size - nail;
count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
p = malloc (count * size);

to determine the size of the char buffer. In the above code is "size"
the size of char buffer.
Could you please let me know what is wrong in the code below.
Is there any other way to place mpz_t variable into char buffer, and
the buffer data back to mpz_t variable.

# include<stdio.h>
# include<gmp.h>
int main(){

int base=10;
mpz_t op;
mpz_t z;
mpz_init_set_str(op,"21896",base);

/*Determinimg the size of the char buffer*/
long size = sizeof(char);
long numb = 8*size - 0;
long count = (mpz_sizeinbase (op, 2) + numb-1) / numb;
char *p = (char *)malloc (count * size);

/* The mpz variable is put into buffer p, and back into mpz variable*/
mpz_export(p,NULL,1,1,0,0,op);
mpz_import (z,count*size , 1, sizeof(p), 0, 0, p);

gmp_printf("the value of z : %Zd\n",z);
}

Since the code is GMP specific, you may have to ask in a GNU or GMP
group or mailing list. Maybe a closer reread of the GMP manual will
help.
 
F

Flash Gordon

Hello,
I am using gmp.h library.

GMP is off topic here, you need to find an appropriate mailing list for
it. However, your code shows some misconceptions about C which I will
point out.

# include<stdio.h>
# include<gmp.h>

You use malloc therefore you should also have
#include said:
int main(){

int base=10;
mpz_t op;
mpz_t z;
mpz_init_set_str(op,"21896",base);

/*Determinimg the size of the char buffer*/
long size = sizeof(char);

sizeof(char) is 1 by definition.
long numb = 8*size - 0;
long count = (mpz_sizeinbase (op, 2) + numb-1) / numb;
char *p = (char *)malloc (count * size);

You do not need to cast the value returned by malloc. I'm guessing you
added the cast because the compiler was complaining. However, the
compiler was not complaining because you needed a cast but rather it was
complaining because you had not included stdlib.h

NEVER add in a cast to shut up a compiler warning. Casts are
occasionally required, but you should only use them when you fully
understand why you need one in a specific place.
 
V

vaidehikedlaya

Thanks for pointing out the error. I actually meant to mention the GMP
library instead of the gmp.h header file. I have already tried posting
the problem to GMP mailing list, but the mailing list is unavaliable
at the moment.
 
S

santosh

(e-mail address removed) wrote:

Please don't top-post. [corrected here]
[ ... ]

I'll point out a few suspicious situations in your code below. Beyond
that you'll have to hope that a gmp expert reads your article.

Check all these mpz_XX functions for errors. That'll help you narrow
down your search.

In C sizeof(char) is always one. For holding raw data, always use
arrays of unsigned char, not plain char.

If you're going to store the number in decimal you might want to
specify 10 instead of 2 to mpz_sinebase.


Don't cast the return value of malloc in C.

Is there a specific reason to supply 1 instead of -1 as the third
parameter?

You want sizeof *p not sizeof(p). The latter will give you the size of
the pointer type itself, not what it points to.

<snip>
 
V

vaidehikedlaya

(e-mail address removed) wrote, On 17/02/07 18:01:


GMP is off topic here, you need to find an appropriate mailing list for
it. However, your code shows some misconceptions about C which I will
point out.



You use malloc therefore you should also have




sizeof(char) is 1 by definition.


You do not need to cast the value returned by malloc. I'm guessing you
added the cast because the compiler was complaining. However, the
compiler was not complaining because you needed a cast but rather it was
complaining because you had not included stdlib.h

NEVER add in a cast to shut up a compiler warning. Casts are
occasionally required, but you should only use them when you fully
understand why you need one in a specific place.

Thank you very much for your feedbacks. The program worked by changing
the sizeof(p) to sizeof(*p).
 
C

CBFalconer

Thanks for pointing out the error. I actually meant to mention the GMP
library instead of the gmp.h header file. I have already tried posting
the problem to GMP mailing list, but the mailing list is unavaliable
at the moment.
.... snip ...

Please do not top-post. Your answer belongs after, or intermixed
with, the material you quote, after snipping anything not germane
to your reply. Many people automatically ignore anything
top-posted. See the following links:

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top