GMP库提供了包装C API的大型INT C API和C ++ API。通常,您可以通过执行
mpz_t integ;
mpz_init(integ);
(请参阅)。这样做时,稍后您必须使用 mpz_clear(integ);
释放内存。 C ++ API的 MPZ_CLASS
自动为您处理此DEADLOCATION。
现在,如果要根据现有内存初始化 mpz_t
,并且不想复制内存内容,则可以使用函数 mpz_roinit_n()
(<(<) a href =“ https://gmplib.org/manual/integer-special-functions” rel =“ nofollow noreferrer”> 5.16 Integer Special Functions )用于 xp :
mpz_srcptr mpz_roinit_n(mpz_t x, const mp_limb_t *xp, mp_size_t xs)
这以特殊的方式初始化 x
,因此可以用作其他MPZ函数的纯输入操作数(因此,函数名称中的 ro
)。现在, mpz_clear(integ)
的文档说:
释放X所占用的空间。为所有MPZ_T调用此功能
变量与它们完成后。
我想知道 mpz_t
's是否已通过呼叫 mpz_roinit_n()
来初始化,因为它们不需要对其进行交易。
如果我正确,这也意味着 mpz_roinit_n()
无法与C ++ API的 mpz_class
一起使用,即使 mpz_class.get_mpz_t()
,因为 mpz_class
的destructor总是试图对基础 mpz_t
进行处理,这将导致内存问题。我在这里正确吗?
The GMP library provides a big int C API and a C++ API which wraps the C API. Usually you initialize an mpz_t
struct (C API) by doing
mpz_t integ;
mpz_init(integ);
(see 5.1 Initialization Functions). When doing so, you later have to free the memory with mpz_clear(integ);
. The C++ API's mpz_class
handles this deallocation automatically for you.
Now, if you want to initialize an mpz_t
based on existing memory, and you don't want to copy the memory contents, you can use the function mpz_roinit_n()
(5.16 Integer Special Functions) for a memory area pointed to by xp
:
mpz_srcptr mpz_roinit_n(mpz_t x, const mp_limb_t *xp, mp_size_t xs)
This initializes x
in a special way so it can be used as a read-only input operand (hence the ro
in the function name) to other mpz functions. Now, the documentation of mpz_clear(integ)
says this:
Free the space occupied by x. Call this function for all mpz_t
variables when you are done with them.
I wonder if mpz_t
's which have been initialized with a call to mpz_roinit_n()
are an exemption to this rule, because they don't need to be deallocated.
If I'm correct, this would also mean that mpz_roinit_n()
can't be used with the C++ API's mpz_class
, not even with mpz_class.get_mpz_t()
, because mpz_class
's destructor always tries to deallocate the underlying mpz_t
, and this would cause memory issues. Am I correct here?
发布评论
评论(1)
mpz_clear
对使用mpz_roinit_n
设置的mpz_t
不执行任何操作。所以你不需要调用它,但如果它被调用它仍然是安全的。mpz_clear
does nothing on ampz_t
set withmpz_roinit_n
. So you don't need to call it, but it is still safe if it gets called.