我有一个程序,收到Segmentation Fault 11
通知main
的简单性。这里是我的整个工作的脚本:C++ GMP mpz_init()导致分割错误11
#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;
}
好了,现在我会在开头添加下面两行主(不改变其他任何有关脚本):
int
main (void)
{
mpz_t r; //added this line
mpz_init (r); //and added this line
makeprime();
return 0;
}
现在我的计划不正确执行,也不打印或执行makeprime()
,而是我得到一个Segmentation Fault 11
通知。
什么给?我在这里做错了什么?
@Galik - 我更新了我的OP以包含我的代码的其余部分 – sadmicrowave
如何阅读“mpz_inits”手册? (提示:“NULL终止”) –