According to the manual of GMP, "The functions for integer arithmetic assume that all integer objects are initialized. You do that by calling the function mpz_init."
For example,
int main(){
mpz_t n1, n2, result;
mpz_init_set_str(n1, "465860", 10);
mpz_init_set_str(n2, "167", 10);
mpz_init(result);
mpz_mod(result, n1, n2);
mpz_out_str(stdout, 10, result);
return 0;
}
if you remove mpz_init(result);
from the code above, the compiler will produce an segmentation fault error. So, my question, what exactly is difference between these two type of definition: mpz_t result
and mpz_init(result)
. Why is mpz_init
also required?
mpz_t result
only declares a variable. The type mpz_t
is a typedef for struct __mpz_struct
. This structure contains three variables which are initialized by mpz_init()
. Hope that helps.