2015-06-20 140 views
0

我有一个程序,收到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通知。

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

+0

@Galik - 我更新了我的OP以包含我的代码的其余部分 – sadmicrowave

+0

如何阅读“mpz_inits”手册? (提示:“NULL终止”) –

回答

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

- Function:无效mpz_initmpz_t X

初始化的x,并将其值设定为0。

- Function:无效mpz_initsmpz_t X,。 ...

初始化一个空终止列表的mpz_t变量,并将其值设置为0.

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

编辑:

你忘了初始化种子。在函数makeprime中,你声明unsigned long seed;,但不要为种子分配一个值,因此每次得到相同的数字,因为种子的值对于每次执行都是相同的。

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

seed = (int) time(NULL); 

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

+0

好吧,我提出了你的建议改变,是的。第一个代码结构工作正常,没有任何问题。但是,当我将'mpz_t r;'和'mpz_init(r);'添加到我的'main()'时,'mpz_urandomb'不再正确播种,并且每次程序运行时都会生成相同的编号。 – sadmicrowave

+0

@sadmicrowave看看最新的答案 –

+0

@Galik谢谢。编辑答案。 –