Remember C++ is pass-by-value unless you explictly say you're passing by reference using the &
operator. In store_m()
you are allocating and assigning to m
inside the function. That won't work since you're passing m
by value. As a result the main()
function never sees the assignment to m
since store_m()
has only a local copy of m
. You are therefore passing an uninitialized variable to encrypt()
. Either allocate m
in main()
or declare store_m()
like so:
void store_m( mpt_t*& m, int& size);
BTW: You're not segfaulting at the 4th cout
. You're segfaulting right after when the encrypt()
function prepares to call mpz_powm()
. The actual crash is the dereference m[i]
(since m
is unitialized).