我试图在函数extended_gcd中更改类型为mpz_t的d的值,然后将其传递回我的main函数,并在执行程序时出现分段错误。
int extended_gcd(mpz_t c, mpz_t a, mpz_t b)
{
mpz_t x, lastx, y, lasty, temp, quotient, temp2;
mpz_init(temp);
mpz_init(temp2);
mpz_init(a);
mpz_init(b);
mpz_init(c);
mpz_init(quotient);
mpz_init_set_ui(x,0);
mpz_init_set_ui(lastx,1);
mpz_init_set_ui(lasty,0);
mpz_init_set_ui(y,1);
while(!(mpz_cmp(b, 0)==0))
{
mpz_set(temp, b);
mpz_cdiv_q(quotient, a, b);
mpz_mod(b, a, b);
mpz_set(a, temp);
mpz_set(temp, x);
mpz_mul(temp2, quotient, x);
mpz_sub(x, lastx, temp2);
mpz_set(lastx, temp);
mpz_set(temp, y);
mpz_mul(temp2, quotient, y);
mpz_sub(y, lasty, temp2);
mpz_set(lasty, temp);
}
mpz_set(c, lastx);
mpz_clear(x);
mpz_clear(y);
mpz_clear(lastx);
mpz_clear(lasty);
mpz_clear(temp);
mpz_clear(temp2);
mpz_clear(quotient);
return 0;
}
这是我主要的代码:
extended_gcd(d,e,phi);
printf("d=");
mpz_out_str(stdout, 10, d);
printf("\n");
知道可能出现什么问题,我该如何解决? 谢谢你的时间。
答案 0 :(得分:1)
我看到一个问题:你不应该对extended_gcd()的参数调用mpz_init()。你应该只对在extended_gcd()中声明的mpz_t变量调用mpz_init()/ mpz_clear()。