Best way to import without copy

Julien D Arques acc.gccquestions at
Thu May 4 13:53:41 CEST 2023

Actually, after trying, the mpz_roinit_n won't make it because doing
something like this:

mpz_class tmp;
std::array <u64, 1> m{10};

mpz_roinit_n( tmp.get_mpz_t(), &m[0], m.size());

std::cout << tmp << std::endl;
tmp += 1;
std::cout << tmp << std::endl;

will yield 10 for the first call but a garbage value (I guess) for the

The fact that get_mpz_t() returns a const and the function is called ROINIT
makes me think that it won't make it..

I am surprised GMP doesn't have a no copy mechanism if we can guarantee
that we have a properly made buffer on input, available for output as well.

On Thu, 4 May 2023 at 08:03, Julien D Arques <acc.gccquestions at>

> thanks Marc
> On Thu, 4 May 2023 at 00:25, Marc Glisse <marc.glisse at> wrote:
>> On Wed, 3 May 2023, Julien D Arques wrote:
>> > I currently use mpz_import, but reading the source code, it appears that
>> > gmp copies the already allocated buffer I give it.
>> > That's double the work, and the said buffer is big.
>> Depending on endianness and other criteria, this might not be a simple
>> copy.
>> > Is there a correct/reliable way to avoid that?
>> If you are certain the format is right, you could try mpz_roinit_n.
>> --
>> Marc Glisse

More information about the gmp-discuss mailing list