I was trying to code something like this
....
mpz_class *x = NULL;
mpz_class *lValue = NULL;
....
for(int k = 0; k < 2; k++) {
x = NULL;
lValue = NULL;
x = (mpz_class*) malloc(sizeof(mpz_class) * exponentForFactors[k]);
lValue = (mpz_class*) malloc(sizeof(mpz_class) * exponentForFactors[k]);
rValue = 0;
mpz_class exp = (p-1)/q[k];
mpz_powm(lValue[0].get_mpz_t(), B.get_mpz_t(),exp.get_mpz_t(), p.get_mpz_t()); <- this part
exponentForFactors[k] = {3, 1}
this code will loop twice as k is less than 2.
At first loop, it is ok. there is no error but when it is the second loop, it has this error message where I pointed. malloc: *** error for object 0x6000000000000000: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
I do not understand why this thing takes place only at the second loop? Any suggestion would be grateful. Thanks.
////////////////////////////
even this code(when the second loop)
cout << "lvalue = " << lValue[0] << endl;
has the problem.
Since mpz_class
is a class, and you are clearly coding in C++ (not C, I have changed your language tag accordingly), using the C allocator is improper. You should instead use new[]
or better yet, use std::vector<>
to allocate your array. Although you have allocated memory for your arrays, the objects within them are uninitialized, because their constructors have not been called.