Copying gmp data structures

Jookia contact at
Fri Nov 3 14:13:07 CET 2023

Hello there,

I'm currently working on a toy programming language that uses mini-gmp.
One goal I have for it is to be immutable and for eventually some of its data
to be garbage collected and compacted.

So I have some questions:

1. Is it stable to move an mpz_t or mpq_t struct using memcpy?

Right now mpz_t/mpq_t are structs that point to heap-allocated data. You can
pass them through function calls and do other nice things.

I'm pretty sure if I just copied this to another place in memory it would
work fine. But the documentation is unclear if this is a supported use case.

2. How would I relocate or compact a mpz_t or mpq_t?

I would like to do generational garbage collection one day, and this would
require relocating and compacting the allocated data of of a mpz_t/mpq_t.

I understand there are functions like mpn_copyi but these seem to be for
internal use? I'm really not sure what would need to be done here.


In an ideal world I could use the stack for gmp calculations and manage
the storage of long-term data using my own memory allocator.

I'd be interested to hear if anyone has done something like that.


More information about the gmp-discuss mailing list