c++ GMP mpz_init() causes Segmentation Fault 11 -
i have program receives segmentation fault 11
notice simplicity of main
. here entire working script:
#include <iostream> #include "gmp.h" void makeprime () { // *********************** variable declaration *********************** // // initilize variables gmp class instances mpz_t l, rand; unsigned long seed; // perform inits create variable pointers 0 value mpz_inits(l, rand); //mpz_init(rand); // calculate random number floor mpz_ui_pow_ui(l, 2, 199); // initilze state object random generator functions gmp_randstate_t rstate; // initialize state mersenne twister algorithm. algorithm fast , has randomness properties. gmp_randinit_mt(rstate); // create generator seed random engine reference gmp_randseed_ui(rstate, seed); /* function: int mpz_probab_prime_p (const mpz_t n, int reps) determine whether n prime. return 2 if n prime, return 1 if n prime (without being certain), or return 0 if n composite. */ { // return uniformly distributed random number in range 0 n-1, inclusive. mpz_urandomb(rand, rstate, 310); // add random number low number, make sure random number between low , 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 memory location random generator state gmp_randclear(rstate); // clear memory locations variables used avoid leaks mpz_clear(l); mpz_clear(rand); } int main (void) { makeprime(); return 0; }
ok, i'll add following 2 lines @ beginning of main (not changing else script):
int main (void) { mpz_t r; //added line mpz_init (r); //and added line makeprime(); return 0; }
now program doesn't execute nor print or execute makeprime()
, instead segmentation fault 11
notice.
what gives? doing wrong here?
i trying debug code using gdb
, gave me segfault @ line mpz_inits(l, rand)
. modified line , not give segfault. see if can find reason segfault.
#include <iostream> #include "gmp.h" void makeprime () { // *********************** variable declaration *********************** // // initilize variables gmp class instances mpz_t l, rand; unsigned long seed; // perform inits create variable pointers 0 value //mpz_inits(l, rand); mpz_init(rand); mpz_init(l); // calculate random number floor mpz_ui_pow_ui(l, 2, 199); // initilze state object random generator functions gmp_randstate_t rstate; // initialize state mersenne twister algorithm. algorithm fast , has randomness properties. gmp_randinit_mt(rstate); // create generator seed random engine reference gmp_randseed_ui(rstate, seed); /* function: int mpz_probab_prime_p (const mpz_t n, int reps) determine whether n prime. return 2 if n prime, return 1 if n prime (without being certain), or return 0 if n composite. */ { // return uniformly distributed random number in range 0 n-1, inclusive. mpz_urandomb(rand, rstate, 310); // add random number low number, make sure random number between low , 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 memory location random generator state gmp_randclear(rstate); // clear memory locations variables used avoid leaks mpz_clear(l); mpz_clear(rand); } int main (void) { makeprime(); return 0; }
edit:
i looked @ gmp documentation , says following mpz_inits
.
— function: void mpz_init (mpz_t x)
initialize x, , set value 0.
— function: void mpz_inits (mpz_t x, ...)
initialize null-terminated list of mpz_t variables, , set values 0.
you must end list of variables want initialize null
. need replace mpz_inits(l, rand)
mpz_inits(l, rand, null)
, works fine.
edit:
you forgot initialize seed. in function makeprime
declare unsigned long seed;
not assign value seed hence same numbers every time because value of seed same every execution.
typically random seeds initialized using system time. can use
seed = (int) time(null);
to initialize seed after declaration. works fine (don't forget include ctime
or time.h
).
Comments
Post a Comment