我有一个程序收到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的开头添加以下2行(不更改脚本的其他任何内容):
int
main (void)
{
mpz_t r; //added this line
mpz_init (r); //and added this line
makeprime();
return 0;
}
现在我的程序没有正确执行,也没有打印或执行makeprime()
,而是收到Segmentation Fault 11
通知。
是什么给出的?我在这里做错了什么?
答案 0 :(得分:1)
我尝试使用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
。
- 功能:无效 mpz_init ( mpz_t x )
初始化x,并将其值设置为0.
- 功能:无效 mpz_inits ( mpz_t x,... )
初始化一个以NULL结尾的mpz_t变量列表,并将其值设置为0.
您必须使用NULL
结束要初始化的变量列表。因此,您需要将mpz_inits(l, rand)
替换为mpz_inits(l, rand, NULL)
,这样就可以了。
修改强>
你忘了初始化种子。在函数makeprime
中,您声明unsigned long seed;
但不为种子赋值,因此每次都会得到相同的数字,因为每次执行时种子的值都相同。
通常使用系统时间初始化随机种子。你可以使用
seed = (int) time(NULL);
在声明之后初始化种子。一切正常(不要忘记包括ctime
或time.h
)。