gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:12:38 29 4
gpt4 key购买 nike

我有一个程序接收到 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) 替换为 mpz_inits(l, rand, NULL),它工作得很好。

编辑:

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

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

seed = (int) time(NULL);

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

关于c++ GMP mpz_init() 导致段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949792/

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