Usage of mpz_t inside of structures

Bruno Jiménez brunojimen at
Wed Jun 20 11:54:53 CEST 2012

El mié, 20-06-2012 a las 11:23 +0200, Torbjorn Granlund escribió:
> Bruno Jiménez <brunojimen at> writes:
>   And I get back all the numbers needed for the key. But I wanted to
>   change it to use directly the structure I have written above, supposing
>   that it would work the same way as with just the mpz_t's, but it
>   doesn't.
>   So, I don't know if I just am doing something wrong, or it is because
>   this just can't be done this way.
> There is no reason why GMP's types couldn't be put inside structures.
> I have no idea about what mistake you have made.

That's what I thought, and why I wanted to organize all the numbers for
the rsa key inside the structure. So I was asking because it was curious
for me that if I pass the mpz as a parameter it stays changed, but if I
pass them inside the structure they don't. As a "test" I have a function
that prints the rsa key, and if I call it at the end of the function
that makes the keys and it outputs one, and calling it after that it
gives me a segmentation fault.

If it helps I'll leave here part of the code:

/* A function to complete the key if we get
 * one of the factors */
void complete_rsa_key(rsa_key pub, mpz_t p)
    /* init and calculation of the mpz_t's
     * inside the rsa_key */

    print_rsa_key(pub); /* This one prints the key completely */

But, if I do in the main:

    print_rsa_key(test_key); /* This one gives the seg fault */

I have also tried to initialize the mpz_t's of the structure before
calling the complete_rsa_key function, and then it doesn't blow up with
the seg fault, but prints only 0s.

There's really no problem for me in making the function return a new key
instead of changing the one given. But I wanted to "keep the style" of
the GMP lib and was curious about what I was doing wrong.

Thanks for all the help!

More information about the gmp-discuss mailing list