Search code examples
c++gmp

C++ GMP, why does this throw an error?


mpz_t* myArr= new mpz_t[M+1];
cout << myArr[0] << endl;
cin.get(); //so I know the program pauses here if everything's OK so far

M is a long long datatype.

I've also tried

mpz_t* myArr= new mpz_t[M+1];
mpz_set_si(myArr[0],0);
cout << myArr[0] << endl;
cin.get(); //so I know the program pauses here if everything's OK so far

Just to give it a value but it does not work still.

Runtime crash


Solution

  • You have to initialize mpz_t values, which are just plain C structures with the GMP C API. If you want to use a class with constructors, use mpz_class, which is a C++ class.

    Example:

    mpz_class x;
    x = 3;
    mpz_class y;
    y = x * 7;