我有一個程序,收到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終止」) –