GMP学习:初始化mpz数组

GMP库中有函数 mpz_array_init ,但是注释了

This is an obsolete function. Do not use it.

mpz_array_init 的问题在于它永远不会释放分配的内存。

看到网上没有什么有关的资料,因此记录一下使用 GMP 初始化数组的方法:

#include 
#include 

int main(void) 
{
     
    int len = 4;
    mpz_t num_arr[len];

    for (int i = 0; i < len; i++) {
     
        mpz_init2(num_arr[i], 1024);  // Initialize x, with space for n-bits numbers, and set its value to 0.
        gmp_scanf("%Zd", num_arr[i]);
    }
    
    for (int i = 0; i < len; i++) {
     
        gmp_printf("%Zd\n", num_arr[i]);
        mpz_clear(num_arr[i]);  // Free the space occupied by num_arr[i].
    }

    return 0;
}

参考

GMP mpz_array_init is an obsolete function - How should we initialize mpz arrays?

你可能感兴趣的:(GMP,学习笔记)