gmp中mpz_t是指针?结构体?或者是其它什么东西?
如果是个结构体,那怎么这样改变变量的值呢:mpz_t a,b,c;mpz_add(a,b,c);此时a就等于b+c了可如果mpz_t声明的是指针,那它的空间是什么时候分配的呢? 指针。 mpz_init初始化 可是mpz_init(a);的参数也是mpz_t型的,这样的话mpz_init()得到的就是一个指针的副本,它如何做到使原指针指向自己开辟的空间呢?
比如下面的代码是会出错的#include <stdio.h>#include <stdlib.h>void f(int *x){ x=(int *)malloc(sizeof(int));}int main(){ int *a; f(a); *a=3; printf("%d",*a); return 0;} 有几种不同的做法,可以通过宏来做到 感谢mathe的解答 5# 没——问题
请教一个问题,自己分配空间自己再free 有什么好处吗。
==================================================
GMP里面有一大堆的 初始化,赋值的函数可以用的,如 mpz_init,mpz_inits,
甚至,初始化和赋值合二为一,一个函数就能搞定的,比如:
mpz_init_set ,mpz_init_set_ui,mpz_init_set_si,mpz_init_set_d,mpz_init_set_str
我感觉GMP提供的这些函数都已经很充分了。 回wayne兄...
没想自己弄,只是用着用着对mpz_t的性质起了疑心,翻代码没翻出头绪就来请教了 7# 没——问题
从gmp.h的代码片段#ifdef __GMP_SHORT_LIMBtypedef unsigned int mp_limb_t;typedef int mp_limb_signed_t;#else#ifdef _LONG_LONG_LIMBtypedef unsigned long long int mp_limb_t;typedef long long int mp_limb_signed_t;#elsetypedef unsigned long int mp_limb_t;typedef long int mp_limb_signed_t;#endif#endiftypedef unsigned long int mp_bitcnt_t;/* For reference, note that the name __mpz_struct gets into C++ mangled function names, which means although the "__" suggests an internal, we must leave this name for binary compatibility.*/typedef struct{int _mp_alloc; /* Number of *limbs* allocated and pointed to by the _mp_d field.*/int _mp_size; /* abs(_mp_size) is the number of limbs the last field points to.If _mp_size is negative this is a negative number.*/mp_limb_t *_mp_d; /* Pointer to the limbs.*/} __mpz_struct;#endif /* __GNU_MP__ */typedef __mpz_struct MP_INT; /* gmp 1 source compatibility */typedef __mpz_struct mpz_t;可以看出定义:mpz_t a,b,c;
就是定义结构体类型的指针变量。
内存管理通过三个函数malloc(), realloc(),free()
__mpz_struct结构体中,_mp_d为数据块指针,_mp_alloc为分配的数据块大小,_mp_size为实际有效数据大小和正负空信息。
init(); 根据_mp_alloc大小(初始化为1)通过malloc()分配空间,并赋块指针给_mp_d,并置_mp_size=0
mpn为通用模块(给mpz,mpf,mpq调用),mpz为整形大数模块,mpf为浮点大数模块,mpq为有理大数模块。
页:
[1]