GMP mpz

编程入门 行业动态 更新时间:2024-10-27 22:26:30
GMP mpz_array_init是一个过时的函数 - 我们应该如何初始化mpz数组?(GMP mpz_array_init is an obsolete function - How should we initialize mpz arrays?)

我只使用了几次GNU MP Bignum库 ,我有兴趣看到我以前分配/启动数组的方式现在已经过时了。 从整数特殊功能 :

5.16特殊功能

本节中的功能用于各种特殊目的。 大多数应用程序不需要它们。 - 功能:void mpz_array_init(mpz_t integer_array,mp_size_t array_size,mp_size_t fixed_num_bits)

This is an obsolete function. Do not use it.

这就是我分配和初始化mpz_t数组的mpz_t 。

int array_size = 100; mpz_t *num_arr; num_arr = malloc(arr_size * sizeof(mpz_t)); mpz_array_init(*num_arr, array_size, 1024);

这仍然可以没有和错误或警告,顺便说一句,但现在这个功能被列为过时,在C中使用GMP分配数组的正确方法是什么?

Having only used the GNU MP Bignum Library a few times, I was interested to see that the way I had previously allocated/initiated arrays is now obsolete. From Integer Special Functions:

5.16 Special Functions

The functions in this section are for various special purposes. Most applications will not need them. — Function: void mpz_array_init (mpz_t integer_array, mp_size_t array_size, mp_size_t fixed_num_bits)

This is an obsolete function. Do not use it.

This is how I would allocate and initialze an array of mpz_t.

int array_size = 100; mpz_t *num_arr; num_arr = malloc(arr_size * sizeof(mpz_t)); mpz_array_init(*num_arr, array_size, 1024);

This still works without and error or warning, btw, but now that this function is listed as obsolete, what is the proper way to allocate an array using GMP in C?

最满意答案

只需循环遍历数组元素并使用mpz_init2初始化它们,如果你想预先分配内存:

for (i = 0; i < array_size; i++) { mpz_init2(num_arr[i], 1024); }

mpz_array_init的问题在于它永远不会释放分配的内存。 如果您单独初始化元素,则可以在以后释放它们:

for (i = 0; i < array_size; i++) { mpz_clear(num_arr[i]); }

Simply loop over the array elements and initialize them one by one using mpz_init2 if you want to preallocate memory:

for (i = 0; i < array_size; i++) { mpz_init2(num_arr[i], 1024); }

The problem with mpz_array_init is that it would never release the allocated memory. If you initialize the elements separately, you can free them afterwards:

for (i = 0; i < array_size; i++) { mpz_clear(num_arr[i]); }

更多推荐

本文发布于:2023-08-04 07:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GMP   mpz

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!