为什么在使用 GMP 时出现 Segmentation fault?

Why is Segmentation fault when using GMP?

我正在使用 GMP。我的程序可以成功构建,但是 运行 失败了。以下是错误的东西:

a=1231231231231231
res^n != a
Segment fault

我程序中的所有代码是:

#include <gmp.h>
#include <stdio.h>
int main()
{
    mpz_t a,res;
    unsigned long int n = 123;

    char str1[] = "1231231231231231";
    mpz_init_set_str(a, str1, 10);
    gmp_printf("a=%Zd\n",a);


    mpz_init(res);

    if(mpz_root(res, a, n)){
        printf("res^n == a\n");
    }
    else{
        printf("res^n != a\n");
    }

    mpz_clears(a,res);
    return 0;
}

您必须像这样调用 mpz_clears():

mpz_clears(a,res, NULL);

文档是这样说的:

Function: void mpz_clears (mpz_t x, ...)

    Free the space occupied by a NULL-terminated list of mpz_t variables.