Node:Memory Management, Next:, Previous:Parameter Conventions, Up:GMP Basics



Memory Management

The GMP types like mpz_t are small, containing only a couple of sizes, and pointers to allocated data. Once a variable is initialized, GMP takes care of all space allocation. Additional space is allocated whenever a variable doesn't have enough.

mpz_t and mpq_t variables never reduce their allocated space. Normally this is the best policy, since it avoids frequent reallocation. Applications that need to return memory to the heap at some particular point can use mpz_realloc2, or clear variables no longer needed.

mpf_t variables, in the current implementation, use a fixed amount of space, determined by the chosen precision and allocated at initialization, so their size doesn't change.

All memory is allocated using malloc and friends by default, but this can be changed, see Custom Allocation. Temporary memory on the stack is also used (via alloca), but this can be changed at build-time if desired, see Build Options.