gpt4 book ai didi

c - GMP GNU 代码错误?

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:45 24 4
gpt4 key购买 nike

当我运行这段代码时,出现错误“Segmentation fault (core dumped)”。但不使用mpz_get_str,没问题。

#include<stdio.h>
#include<gmp.h>
int main()
{
char *A;
mpz_t P,Q;
mpz_init(P);
mpz_init(Q);
mpz_set_str(P,"1201858877187548528922917",10);
mpz_set_str(Q,"1248833599132922783100713",10);

mpz_t PHI,E,K,d;
mpz_init(PHI);
mpz_init(E);
mpz_init(K);
mpz_init(d);

mpz_sub_ui(PHI,P,1);
mpz_sub_ui(K,Q,1);
mpz_mul(PHI,PHI,K);
mpz_set_str(E,"37",10);
mpz_gcd(d,E,PHI);
gmp_printf("d=%Zd\n",d);
mpz_invert(d,E,PHI);
gmp_printf("d=%Zd\n",d);
A=mpz_get_str(A,2,d);

mpz_mul(K,d,E);
mpz_sub_ui(K,K,1);
mpz_divexact(K,K,PHI);



}

最佳答案

你应该将你的 char *A 初始化为 NULL 或 0,否则(如果它不是 NULL 或 0 偶然),GMP 将尝试写入一个随机内存地址,这将导致段错误.

来自 GMP 文档:http://gmplib.org/manual/Converting-Integers.html

If str is NULL, the result string is allocated using the current allocation function (see Custom Allocation). The block will be strlen(str)+1 bytes, that being exactly enough for the string and null-terminator.

If str is not NULL, it should point to a block of storage large enough for the result, that being mpz_sizeinbase (op, base) + 2. The two extra bytes are for a possible minus sign, and the null-terminator.

关于c - GMP GNU 代码错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com