c++ GMP mpz_init() 导致分段错误 11

c++ GMP mpz_init() causes Segmentation Fault 11

我有一个程序收到 Segmentation Fault 11 通知 main 的简单性。这是我的整个工作脚本:

#include <iostream>
#include "gmp.h"

void 
makeprime ()
{
    // *********************** VARIABLE DECLARATION *********************** //
    // initilize the variables as gmp class instances
    mpz_t l, rand;
    unsigned long seed;
    // perform inits to create variable pointers with 0 value
    mpz_inits(l, rand);
    //mpz_init(rand);

    // calculate the random number floor
    mpz_ui_pow_ui(l, 2, 199);

    // initilze the state object for the random generator functions
    gmp_randstate_t rstate;
    // initialize state for a Mersenne Twister algorithm. This algorithm is fast and has good randomness properties.
    gmp_randinit_mt(rstate);

    // create the generator seed for the random engine to reference 
    gmp_randseed_ui(rstate, seed);

    /*
    Function:
    int mpz_probab_prime_p (const mpz_t n, int reps)

    Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), 
    or return 0 if n is definitely composite.
    */
    do {
        // return a uniformly distributed random number in the range 0 to n-1, inclusive.
        mpz_urandomb(rand, rstate, 310);

        // add the random number to the low number, which will make sure the random number is between the low and high ranges
        mpz_add(rand, rand, l);

        gmp_printf("randomly generated number: %Zd\n", rand);

    } while ( !(mpz_probab_prime_p(rand, 25)) );        

    // *********************** GARBAGE COLLECTION *********************** //
    // empty the memory location for the random generator state
    gmp_randclear(rstate);
    // clear the memory locations for the variables used to avoid leaks
    mpz_clear(l);
    mpz_clear(rand);
}

int
main (void)
{
    makeprime();
    return 0;
}

好的,现在我将在 main 的开头添加以下两行(不更改脚本的任何其他内容):

int
main (void)
{
    mpz_t r;      //added this line
    mpz_init (r); //and added this line

    makeprime();
    return 0; 
}

现在我的程序没有正确执行,也没有打印或执行 makeprime(),而是我收到 Segmentation Fault 11 通知。

什么给了?我在这里做错了什么?

我试图使用 gdb 调试您的代码,但它在第 mpz_inits(l, rand) 行给了我一个段错误。然后我修改了那行,现在它没有给出段错误。我会看看是否能找到段错误的原因。

#include <iostream>
#include "gmp.h"

void 
makeprime ()
{
    // *********************** VARIABLE DECLARATION *********************** //
    // initilize the variables as gmp class instances
    mpz_t l, rand;
    unsigned long seed;
    // perform inits to create variable pointers with 0 value

    //mpz_inits(l, rand);
    mpz_init(rand);
    mpz_init(l);

    // calculate the random number floor
    mpz_ui_pow_ui(l, 2, 199);

    // initilze the state object for the random generator functions
    gmp_randstate_t rstate;
    // initialize state for a Mersenne Twister algorithm. This algorithm is fast and has good randomness properties.
    gmp_randinit_mt(rstate);

    // create the generator seed for the random engine to reference 
    gmp_randseed_ui(rstate, seed);

    /*
    Function:
    int mpz_probab_prime_p (const mpz_t n, int reps)

    Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), 
    or return 0 if n is definitely composite.
    */
    do {
        // return a uniformly distributed random number in the range 0 to n-1, inclusive.
        mpz_urandomb(rand, rstate, 310);

        // add the random number to the low number, which will make sure the random number is between the low and high ranges
        mpz_add(rand, rand, l);

        gmp_printf("randomly generated number: %Zd\n", rand);

    } while ( !(mpz_probab_prime_p(rand, 25)) );        

    // *********************** GARBAGE COLLECTION *********************** //
    // empty the memory location for the random generator state
    gmp_randclear(rstate);
    // clear the memory locations for the variables used to avoid leaks
    mpz_clear(l);
    mpz_clear(rand);
}

int
main (void)
{
    makeprime();
    return 0;
}

编辑:

我查看了 GMP Documentation,它说了以下关于 mpz_inits 的内容。

— Function: void mpz_init (mpz_t x)

Initialize x, and set its value to 0.

— Function: void mpz_inits (mpz_t x, ...)

Initialize a NULL-terminated list of mpz_t variables, and set their values to 0.

您必须以 NULL 结束要初始化的变量列表。所以你需要用 mpz_inits(l, rand, NULL) 替换 mpz_inits(l, rand) 并且它工作得很好。

编辑:

您忘记初始化种子。在函数 makeprime 中,您声明了 unsigned long seed; 但没有为种子赋值,因此您每次都会得到相同的数字,因为每次执行的种子值都相同。

通常使用系统时间初始化随机种子。您可以使用

seed = (int) time(NULL);

在声明后立即初始化种子。然后一切正常(不要忘记包括 ctimetime.h)。