By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Feb 17 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
va************@gmail.com wrote:
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.

Feb 17 '07 #2

P: n/a
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.

On Feb 17, 11:08 am, "santosh" <santosh....@gmail.comwrote:
vaidehikedl...@gmail.com wrote:
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.

Feb 17 '07 #3

P: n/a
va************@gmail.com wrote, On 17/02/07 18:01:
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.

<snip>
# include<stdio.h>
# include<gmp.h>
You use malloc therefore you should also have
#include <stdlib.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);
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.
--
Flash Gordon
Feb 17 '07 #4

P: n/a
va************@gmail.com wrote:

Please don't top-post. [corrected here]
On Feb 17, 11:08 am, "santosh" <santosh....@gmail.comwrote:
vaidehikedl...@gmail.com wrote:
[ ... ]

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.
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);
Check all these mpz_XX functions for errors. That'll help you narrow
down your search.
/*Determinimg the size of the char buffer*/
long size = sizeof(char);
In C sizeof(char) is always one. For holding raw data, always use
arrays of unsigned char, not plain char.
long numb = 8*size - 0;
long count = (mpz_sizeinbase (op, 2) + numb-1) / numb;
If you're going to store the number in decimal you might want to
specify 10 instead of 2 to mpz_sinebase.

char *p = (char *)malloc (count * size);
Don't cast the return value of malloc in C.
/* The mpz variable is put into buffer p, and back into mpz variable*/
mpz_export(p,NULL,1,1,0,0,op);
Is there a specific reason to supply 1 instead of -1 as the third
parameter?
mpz_import (z,count*size , 1, sizeof(p), 0, 0, p);
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>

Feb 17 '07 #5

P: n/a
On Feb 17, 11:26 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
vaidehikedl...@gmail.com wrote, On 17/02/07 18:01:
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.

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

You use malloc therefore you should also have
#include <stdlib.h>
int main(){
int base=10;
mpz_top;
mpz_tz;
mpz_init_set_str(op,"21896",base);
/*Determinimg the size of thecharbuffer*/
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.
--
Flash Gordon
Thank you very much for your feedbacks. The program worked by changing
the sizeof(p) to sizeof(*p).

Feb 17 '07 #6

P: n/a
va************@gmail.com wrote:
>
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.

On Feb 17, 11:08 am, "santosh" <santosh....@gmail.comwrote:
>vaidehikedl...@gmail.com wrote:
>>I am using gmp.h library.

No such library. It's a header file, presumably for the GMP library.
.... 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)
Feb 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.